标签:main red you head 要求 memset tran input nes
1 #include<iostream> 2 #include<string.h> 3 #include<cstdio> 4 using namespace std; 5 const int maxn=1e5+5; 6 struct EDGE{int to,next;}edge[maxn<<1]; 7 int t,n,m,k,x,y,cnt,res,maxdist,head[maxn]; 8 void add_edge(int u,int v){ 9 edge[cnt].to=v; 10 edge[cnt].next=head[u]; 11 head[u]=cnt++; 12 } 13 int dfs(int u,int fa,int &maxdist){ 14 int Dmax=0,Dsec=0; 15 for(int i=head[u];~i;i=edge[i].next){ 16 int v=edge[i].to; 17 if(v^fa){ 18 int nowd=dfs(v,u,maxdist)+1; 19 if(nowd>Dmax)Dsec=Dmax,Dmax=nowd; 20 else if(nowd>Dsec)Dsec=nowd; 21 } 22 } 23 maxdist=max(maxdist,Dmax+Dsec); 24 return Dmax; 25 } 26 int main(){ 27 while(~scanf("%d",&t)){ 28 while(t--){ 29 scanf("%d%d",&n,&m); 30 memset(head,-1,sizeof(head));cnt=maxdist=0; 31 while(--n){ 32 scanf("%d%d",&x,&y); 33 add_edge(x,y); 34 add_edge(y,x); 35 } 36 dfs(1,-1,maxdist); 37 while(m--){ 38 scanf("%d",&k); 39 if(k<=maxdist)printf("%d\n",k-1); 40 else printf("%d\n",maxdist+(k-maxdist-1)*2); 41 } 42 } 43 } 44 return 0; 45 }
题解报告:hdu 4607 Park Visit(最长路+规律)
标签:main red you head 要求 memset tran input nes
原文地址:https://www.cnblogs.com/acgoto/p/9572853.html