#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define MAXN 20010
using namespace std;
typedef struct{
int to,next;
}Node;
typedef struct PP{
int id,par;
bool operator < (const PP &a) const {
if(par == a.par) return id < a.id;
return par < a.par;
}
}Partition;
Node edge[MAXN*2];
Partition P[MAXN];
int head[MAXN],vis[MAXN],cnt[MAXN],N;
void addedge(int u,int v,int k){
edge[k].to = v;
edge[k].next = head[u];
head[u] = k;
}
int dfs(int s){
vis[s] = cnt[s] = 1;
for(int i = head[s];~i;i = edge[i].next){
int u = edge[i].to;
if(!vis[u]) cnt[s] += dfs(u);
}
return cnt[s]--;
}
void dfs_partition(int s){
vis[s] = 1;
P[s].id =s;
P[s].par = N - cnt[s] - 1;
for(int i = head[s];~i;i = edge[i].next){
int u = edge[i].to;
if(!vis[u]){
P[s].par = max(P[s].par,cnt[u]+1);
dfs_partition(u);
}
}
}
int main(){
int t,u,v;
// freopen("in.c","r",stdin);
scanf("%d",&t);
while(t--){
memset(head,-1,sizeof(head));
scanf("%d",&N);
int k = 1;
for(int i = 1;i <= N-1;i ++){
scanf("%d%d",&u,&v);
addedge(u,v,k++);
addedge(v,u,k++);
}
memset(vis,0,sizeof(vis));
dfs(1);
memset(vis,0,sizeof(vis));
dfs_partition(1);
sort(P+1,P+N+1);
printf("%d %d\n",P[1].id,P[1].par);
}
return 0;
}
POJ 1655
最新推荐文章于 2019-08-13 21:52:00 发布