标签:
10 1 2 2 3 3 4 4 5 6 7 7 8 8 9 9 10 3 8
3 8
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <algorithm> 6 using namespace std; 7 const int maxn = 10010; 8 vector<int>g[maxn],ans; 9 int n,m,son[maxn]; 10 void dfs(int u,int fa){ 11 son[u] = 1; 12 int maxson = 0; 13 for(int i = g[u].size()-1; i >= 0; --i){ 14 if(g[u][i] == fa) continue; 15 dfs(g[u][i],u); 16 maxson = max(maxson,son[g[u][i]]); 17 son[u] += son[g[u][i]]; 18 } 19 maxson = max(maxson,n - son[u]); 20 if(maxson <= (n>>1)) ans.push_back(u); 21 } 22 int main(){ 23 int u,v; 24 while(~scanf("%d",&n)){ 25 for(int i = 0; i <= n; ++i) g[i].clear(); 26 for(int i = 1; i < n; ++i){ 27 scanf("%d%d",&u,&v); 28 g[u].push_back(v); 29 g[v].push_back(u); 30 } 31 ans.clear(); 32 dfs(1,-1); 33 if(ans.size()){ 34 sort(ans.begin(),ans.end()); 35 for(int i = 0; i < ans.size(); ++i) 36 printf("%d\n",ans[i]); 37 }else puts("NONE"); 38 } 39 return 0; 40 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4797914.html