先dfs球每个点的子树总结点数(包括他自己)。。然后就显然了。。
# include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <vector> using namespace std; int d[20000],pa[20000]; typedef struct NODE{ int k;struct NODE* next; }Node; Node edge[2*20000],*G[10001],*tail = edge; void add(int a,int b) { tail->k = b;tail->next = G[a];G[a] = tail++; } int dfs(int u,int fa) { d[u] = 1; pa[u] = fa; Node* i; for (i=G[u];i;i=i->next) if (i->k!=fa) d[u] += dfs(i->k,u); return d[u]; } int main() { int n,a,b,i,cnt=0; Node* p; scanf("%d",&n); for (i=0;i<n-1;++i) { scanf("%d%d",&a,&b); add(a,b); add(b,a); } n = dfs(1,0); for (i=1;i<=n;++i) { int flag = 1; for (p=G[i];p;p=p->next) if ((p->k)!=pa[i]&&d[p->k]>(n/2)) flag = 0; if ((n-d[i])>(n/2)) flag = 0; if (flag) { printf("%d ",i); cnt++; } } if (!cnt) printf("NONE "); return 0; }