Description
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T.
For example, consider the tree:
Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.
For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.
For example, consider the tree:
Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.
For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.
题意:给出一棵树,求该树的重心。
解法:算是模板题了,已知以重心为根,其儿子子树的最大SIZE最小,那么可以考虑树形DP,用一个MAXV[]去记录以X为重心时,儿子子树的最大SIZE。复杂度为O(N)
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define NN 20020
int n;
int ptx[NN],lnum;
struct edge{
int v,next,w;
edge(){}
edge(int v,int next,int w){
this->v=v;
this->next=next;
this->w=w;
}
}ed[NN*2];
void addline(int x,int y,int w){
ed[lnum]=edge(y,ptx[x],w);
ptx[x]=lnum++;
}
int f[NN],sz[NN];
int dfs(int x,int fa){
f[x]=fa;
sz[x]=0;
gson(i,x){
int y=ed[i].v;
if(y==fa)continue;
sz[x]+=dfs(y,x);
}
sz[x]++;
return sz[x];
}
int maxv[NN],maxval,center;
void getCenter(int r,int x){
maxv[x]=0;
gson(i,x){
int y=ed[i].v;
if(y==f[x])continue;
getCenter(r,y);
maxv[x]=max(maxv[x],sz[y]);
}
maxv[x]=max(maxv[x],sz[r]-sz[x]);
if(maxv[x]<maxval||(maxv[x]==maxval&&x<center))
center=x,maxval=maxv[x];
}
inline int getCenter(int x){
maxval=inf;
getCenter(x,x);
return center;
}
void init(){
lnum=0;
mem(ptx,-1);
}
int main(){
tdata{
init();
scanff(n);
rep(i,1,n-1){
int x,y;
scanff(x);
scanff(y);
addline(x,y,1);
addline(y,x,1);
}
dfs(1,0);
int ans=getCenter(1);
printf("%d %d\n",ans,maxv[ans]);
}
}
/*
3
5
1 2
2 3
3 4
4 5
7
2 6
1 2
1 4
4 5
3 7
3 1
4
1 2
2 3
3 4
*/