#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#define inf 1E9#define maxn 10001using namespace std;vector<int> tree[maxn];int n;int zx=0,minzs=inf;int d[maxn];void dfs(int x, int fa) { d[x]=1; int maxzs=0; for(int i=0; i<tree[x].size(); ++i) { int to=tree[x][i]; if(to!=fa) { dfs(to,x); maxzs=max(maxzs,d[to]); d[x]+=d[to]; } } maxzs=max(maxzs,n-d[x]); if(maxzs<minzs) { minzs=maxzs; zx=x; }}int main() { scanf("%d", &n); for(int i=1; i<=n-1; ++i) { int x,y; scanf("%d%d", &x, &y); tree[x].push_back(y); tree[y].push_back(x); } dfs(1,0); PRintf("%d %d", zx,minzs); return 0;}
新闻热点
疑难解答