#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> V[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 = n - a - b;
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];
vector<int> V1, V2;
auto it = V[u].lower_bound((n + 2) / 3);
if (it != V[u].end()) V1.push_back(*it);
if (it != V[u].begin()) V1.push_back(*prev(it));
it = V[v].lower_bound((n + 2) / 3);
if (it != V[v].end()) V2.push_back(*it);
if (it != V[v].begin()) V2.push_back(*prev(it));
for (int x : V1) for (int y : V2) chk(x, y);
if (V[u].size() < V[v].size()) swap(V[u], V[v]);
for (int x : V[v]) V[u].insert(x);
}
V[u].insert(sz[u]);
auto it = V[u].lower_bound(sz[u] / 2);
chk(*it, sz[u] - *it);
if (it != V[u].begin()) chk(*prev(it), sz[u] - *prev(it));
};
dfs(dfs, 1);
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14336 KB |
Output is correct |
4 |
Correct |
7 ms |
14432 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14336 KB |
Output is correct |
4 |
Correct |
7 ms |
14432 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14548 KB |
Output is correct |
7 |
Correct |
8 ms |
14548 KB |
Output is correct |
8 |
Correct |
8 ms |
14600 KB |
Output is correct |
9 |
Correct |
9 ms |
14548 KB |
Output is correct |
10 |
Correct |
8 ms |
14516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14336 KB |
Output is correct |
4 |
Correct |
7 ms |
14432 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14548 KB |
Output is correct |
7 |
Correct |
8 ms |
14548 KB |
Output is correct |
8 |
Correct |
8 ms |
14600 KB |
Output is correct |
9 |
Correct |
9 ms |
14548 KB |
Output is correct |
10 |
Correct |
8 ms |
14516 KB |
Output is correct |
11 |
Correct |
204 ms |
33484 KB |
Output is correct |
12 |
Correct |
183 ms |
34108 KB |
Output is correct |
13 |
Correct |
160 ms |
33356 KB |
Output is correct |
14 |
Correct |
174 ms |
33768 KB |
Output is correct |
15 |
Correct |
195 ms |
34136 KB |
Output is correct |
16 |
Correct |
133 ms |
32484 KB |
Output is correct |
17 |
Correct |
179 ms |
33512 KB |
Output is correct |
18 |
Correct |
160 ms |
43340 KB |
Output is correct |
19 |
Correct |
171 ms |
34464 KB |
Output is correct |
20 |
Correct |
171 ms |
34252 KB |
Output is correct |