标签:each any http lines fine har highlight cst while
Description
Input
Output
Sample Input
1 7 2 6 1 2 1 4 4 5 3 7 3 1
Sample Output
1 2
树形dp,只需要用到每个子树的大小。
水~
//Serene #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> using namespace std; const int maxn=2e4+10; int T,n,tot; int aa;char cc; int read() { aa=0;cc=getchar(); while(cc<‘0‘||cc>‘9‘) cc=getchar(); while(cc>=‘0‘&&cc<=‘9‘) aa=aa*10+cc-‘0‘,cc=getchar(); return aa; } int fir[maxn],nxt[2*maxn],to[2*maxn],e=0; void add(int x,int y) { to[++e]=y;nxt[e]=fir[x];fir[x]=e; to[++e]=x;nxt[e]=fir[y];fir[y]=e; } struct Node{ int pos,fa,size,maxnum; }node[maxn]; void dfs(int pos) { node[pos].pos=pos; node[pos].size=1; for(int y=fir[pos];y;y=nxt[y]) { if(to[y]==node[pos].fa) continue; node[to[y]].fa=pos;dfs(to[y]); node[pos].maxnum=max(node[pos].maxnum,node[to[y]].size); node[pos].size+=node[to[y]].size; } } bool cmp(const Node& a,const Node& b) { int x=max(a.maxnum,tot-a.size),y=max(b.maxnum,tot-b.size); return x!=y? x<y:a.pos<b.pos; } int main() { T=read(); int x,y; while(T--) { memset(fir,0,sizeof(fir));e=0; memset(node,0,sizeof(node)); n=read(); for(int i=1;i<n;++i) { x=read();y=read(); add(x,y); } dfs(1);tot=node[1].size; sort(node+1,node+n+1,cmp); x=max(node[1].maxnum,tot-node[1].size); printf("%d %d\n",node[1].pos,x); } return 0; }
标签:each any http lines fine har highlight cst while
原文地址:http://www.cnblogs.com/Serene-shixinyi/p/7482045.html