思路:和POJ3107几乎一样...忘记把ans归0了 WA一次...
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 50010
#define inf 1e9
#define LL long long
int cas=1,T;
//vector<int>e[maxn];
int tot[maxn],f[maxn],minx;
int ans;
int n;
struct Edge
{
int v,next;
}e[maxn*2];
int head[maxn];
int len=0;
void add(int u,int v)
{
e[len].v=v;
e[len].next=head[u];
head[u]=len++;
}
void dfs(int u,int fa)
{
tot[u]=1;
for (int i =head[u];i!=-1;i=e[i].next)
{
int v = e[i].v;
if (v==fa)
continue;
dfs(v,u);
tot[u]+=tot[v];
ans = max(ans,tot[v]);
}
f[u]=max(ans,n-tot[u]);
minx = min(minx,f[u]);
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
memset(head,-1,sizeof(head));
len=0;
for (int i = 1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
minx = inf;
ans=0;
dfs(1,-1);
bool first = true;
for (int i = 1;i<=n;i++)
if (f[i]==minx)
{
printf("%d ",i);
break;
}
printf("%d",minx);
printf("\n");
}
//freopen("in","r",stdin);
//scanf("%d",&T);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}