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.

50 lines
1.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
#define int long long
#define endl "\n"
struct edge {
int to, nxt;
} e[N << 1];
int n, cnt, id;
int head[N];
int ans;
int f[N], dep[N], size[N];
inline void add(int u, int v) {
e[++cnt].nxt = head[u];
head[u] = cnt;
e[cnt].to = v;
}
void dfs1(int x, int fa) {
size[x] = 1;
dep[x] = dep[fa] + 1;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa) continue;
dfs1(y, x);
size[x] += size[y];
}
}
void dfs2(int x, int fa) {
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa) continue;
f[y] = f[x] + n - 2 * size[y];
dfs2(y, x);
}
}
signed main() {
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
add(u, v), add(v, u);
}
dfs1(1, 0);
for (int i = 1; i <= n; i++) f[1] += dep[i];
dfs2(1, 0);
for (int i = 1; i <= n; i++)
if (ans < f[i]) ans = f[i], id = i;
cout << id << endl;
}