#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int n, ans = INT_MAX, sz[maxn], fa[maxn];
vector<int> G[maxn];
set<int> S[maxn];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
G[u].push_back(v), G[v].push_back(u);
}
auto chk = [&](int a, int b, int c) {
if (a && b && c) ans = min(ans, max({a, b, c}) - min({a, b, c}));
};
auto dfs = [&](auto self, int u) -> void {
sz[u] = 1;
for (int v : G[u]) if (v ^ fa[u]) {
fa[v] = u, self(self, v), sz[u] += sz[v];
if (S[u].size() < S[v].size()) swap(S[u], S[v]);
for (int x : S[v]) S[u].insert(x);
}
S[u].insert(sz[u]);
auto it = S[u].lower_bound(sz[u] / 2);
chk(*it, sz[u] - *it, n - sz[u]);
if (it != S[u].begin()) chk(*prev(it), sz[u] - *prev(it), n - sz[u]);
};
dfs(dfs, 1);
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14424 KB |
Output is correct |
3 |
Incorrect |
8 ms |
14324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14424 KB |
Output is correct |
3 |
Incorrect |
8 ms |
14324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14424 KB |
Output is correct |
3 |
Incorrect |
8 ms |
14324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |