#include<cstdio>
#include<cstring>
#define MAX(x,y) ((x)>(y)?(x):(y))
struct node
{
int to,next;
}e[41000];
int cnt,head[21000],n;
void add_edge(int from,int to)
{
e[cnt].to=to;
e[cnt].next=head[from];
head[from]=cnt++;
}
int dp[20010];
int num[20010];
void dfs(int u,int v)
{
dp[u]=1;
for(int i=head[u];i!=-1;i=e[i].next)
{
int to=e[i].to;
if(to==v)
continue;
dfs(to,u);
dp[u]+=dp[to];
num[u]=MAX(num[u],dp[to]);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
cnt=0;
memset(head,-1,sizeof(head));
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
memset(num,0,sizeof(num));
dfs(1,-1);
int res=0x3f3f3f3f,lo;
for(int i=1;i<=n;i++)
{
if(res>MAX(n-dp[i],num[i]))
{
lo=i;
res=MAX(n-dp[i],num[i]);
}
}
printf("%d %d\n",lo,res);
}
}
poj 1655 DP
最新推荐文章于 2024-10-01 18:52:56 发布