You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
using namespace std;
|
|
|
|
|
const int N = 1000010, M = N << 1;
|
|
|
|
|
#define int long long
|
|
|
|
|
#define endl "\n"
|
|
|
|
|
|
|
|
|
|
int n, id;
|
|
|
|
|
// 链式前向星
|
|
|
|
|
int e[M], h[N], idx, w[M], ne[M];
|
|
|
|
|
void add(int a, int b, int c = 0) {
|
|
|
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ans;
|
|
|
|
|
int f[N], dep[N], size[N];
|
|
|
|
|
|
|
|
|
|
void dfs1(int u, int fa) {
|
|
|
|
|
size[u] = 1;
|
|
|
|
|
dep[u] = dep[fa] + 1;
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int v = e[i];
|
|
|
|
|
if (v == fa) continue;
|
|
|
|
|
dfs1(v, u);
|
|
|
|
|
size[u] += size[v];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void dfs2(int u, int fa) {
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int v = e[i];
|
|
|
|
|
if (v == fa) continue;
|
|
|
|
|
f[v] = f[u] + n - 2 * size[v];
|
|
|
|
|
dfs2(v, u);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
signed main() {
|
|
|
|
|
memset(h, -1, sizeof h);
|
|
|
|
|
cin >> n;
|
|
|
|
|
for (int i = 1; i < n; i++) { // n-1条边
|
|
|
|
|
int a, b;
|
|
|
|
|
cin >> a >> b;
|
|
|
|
|
add(a, b), add(b, a); // 换根DP,无向图
|
|
|
|
|
}
|
|
|
|
|
// 1、第一次dfs,找出:xxx
|
|
|
|
|
dfs1(1, 0);
|
|
|
|
|
|
|
|
|
|
// 2、换根
|
|
|
|
|
for (int i = 1; i <= n; i++) f[1] += dep[i];
|
|
|
|
|
dfs2(1, 0);
|
|
|
|
|
|
|
|
|
|
// 3、找答案
|
|
|
|
|
for (int i = 1; i <= n; i++)
|
|
|
|
|
if (ans < f[i]) ans = f[i], id = i;
|
|
|
|
|
cout << id << endl;
|
|
|
|
|
}
|