标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9965 | Accepted: 4087 |
Description
Input
Output
Sample Input
1 7 2 6 1 2 1 4 4 5 3 7 3 1
Sample Output
1 2
Source
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<cstdlib> #include<string> #include<vector> #define INF 100000000 using namespace std; vector<int> e[20005]; int n,dp[20005],ans,num; void dfs(int u,int father) { int maxx=0; dp[u]=1; for(int i=0;i<e[u].size();i++) { int v=e[u][i]; if(v==father) continue; dfs(v,u); dp[u]+=dp[v]; maxx=max(maxx,dp[v]); } maxx=max(maxx,n-dp[u]); if(maxx==ans) { if(u<num) num=u; } if(maxx<ans) { ans=maxx; num=u; } } int main() { int tt; scanf("%d",&tt); while(tt--) { scanf("%d",&n); for(int i=1;i<=n;i++) e[i].clear(); num=1; ans=INF; for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); e[x].push_back(y); e[y].push_back(x); } dfs(1,-1); printf("%d %d\n",num,ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/water-full/p/4501674.html