POJ3107树的重心
请使用用scanf和printf谢谢。关闭输入输出流还是t
/*I lv ya*/
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<char,char> PCC;
#define fi first
#define se second
#define delay for(ll DELAY=0;DELAY<=4000000000;DELAY++)
#define pb push_back
bool cmp1(ll x,ll y) {return x>y;}
const ll mod = 1e9+7;
const int N = 2e5+5;
int mx[] = { 1,0,-1,0 }, my[] = { 0,1,0,-1 };
///list star startint head[N<<2], IDX = 0;struct NODE{int t, ne, w;}ed[N<<2];
void add(int s,int t){ed[++IDX].ne = head[s]; ed[IDX].t = t; head[s] = IDX;
}///list star end
/// DSU_begin
/*
int FA[N];
int _fi(int x){return (FA[x] == x) ? x : FA[x] = _fi(FA[x]);}
void uni(int x, int y){int xx = _fi(x), yy = _fi(y);if (xx != yy) FA[xx] = yy;}
*/
/// DSU_end
int n;
int d[N];///当前以i为根的子树的点数
int maxn = 0x3f3f3f3f;
int cnt;
int ans[N];
void dfs(int now,int last)
{d[now] = 1;int tmp = 0;///当前结点的最大子树大小for(int i = head[now];i;i=ed[i].ne){int t = ed[i].t;if(t!=last){dfs(t,now);///向下遍历,向上传递d[now]+=d[t];tmp = max(tmp,d[t]);}}tmp = max(tmp,n-d[now]);///父节点为根、不涉及当前结点分支的子树的节点数if(tmp<maxn){maxn = tmp;cnt = 0;ans[++cnt] = now;}else if(tmp == maxn){ans[++cnt] = now;}
}
void solve()
{scanf("%d",&n);for(int i=1;i<=n-1;i++){int a,b;scanf("%d %d",&a,&b);add(a,b);add(b,a);}dfs(1,0);///直接从1结点遍历sort(ans+1,ans+cnt+1);for(int i=1;i<=cnt;i++){printf("%d ",ans[i]);}cout<<endl;
}
int main()
{//ios::sync_with_stdio(false);//cin.tie(NULL),cout.tie(NULL);int _=1;//cin>>_;while(_--){solve();}return 0;
}