#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, sz[N], ans = 1e9;
vector<int> g[N];
void pre_dfs(int v, int p = -1){
sz[v] = 1;
for (int u : g[v]){
if (u == p) continue;
pre_dfs(u, v);
sz[v] += sz[u];
}
}
set<int> anc, non_anc;
void dfs(int v, int p = -1){
auto lb = anc.lower_bound(sz[v] + (n - sz[v]) / 2);
if (lb != anc.end()){
int mx = max({sz[v], (*lb) - sz[v], n - (*lb)});
int mn = min({sz[v], (*lb) - sz[v], n - (*lb)});
ans = min(ans, mx - mn);
}
if (lb != anc.begin()){
lb--;
int mx = max({sz[v], (*lb) - sz[v], n - (*lb)});
int mn = min({sz[v], (*lb) - sz[v], n - (*lb)});
ans = min(ans, mx - mn);
}
lb = anc.lower_bound(sz[v] + (n - sz[v]) / 2 + 1);
if (lb != anc.end()){
int mx = max({sz[v], (*lb) - sz[v], n - (*lb)});
int mn = min({sz[v], (*lb) - sz[v], n - (*lb)});
ans = min(ans, mx - mn);
}
if (lb != anc.begin()){
lb--;
int mx = max({sz[v], (*lb) - sz[v], n - (*lb)});
int mn = min({sz[v], (*lb) - sz[v], n - (*lb)});
ans = min(ans, mx - mn);
}
auto it = non_anc.lower_bound((n - sz[v]) / 2);
if (it != non_anc.end()){
int mx = max({sz[v], (*it), n - (*it) - sz[v]});
int mn = min({sz[v], (*it), n - (*it) - sz[v]});
ans = min(ans, mx - mn);
}
if (it != non_anc.begin()){
it--;
int mx = max({sz[v], (*it), n - (*it) - sz[v]});
int mn = min({sz[v], (*it), n - (*it) - sz[v]});
ans = min(ans, mx - mn);
}
it = non_anc.lower_bound((n - sz[v]) / 2 + 1);
if (it != non_anc.end()){
int mx = max({sz[v], (*it), n - (*it) - sz[v]});
int mn = min({sz[v], (*it), n - (*it) - sz[v]});
ans = min(ans, mx - mn);
}
if (it != non_anc.begin()){
it--;
int mx = max({sz[v], (*it), n - (*it) - sz[v]});
int mn = min({sz[v], (*it), n - (*it) - sz[v]});
ans = min(ans, mx - mn);
}
anc.insert(sz[v]);
for (int u : g[v]){
if (u == p) continue;
dfs(u, v);
}
anc.erase(sz[v]);
non_anc.insert(sz[v]);
}
int main(){
cin >> n;
for (int i = 1; i < n; i ++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
pre_dfs(1);
dfs(1);
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4968 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4968 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4968 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
143 ms |
12244 KB |
Output is correct |
12 |
Correct |
119 ms |
12116 KB |
Output is correct |
13 |
Correct |
145 ms |
12884 KB |
Output is correct |
14 |
Correct |
125 ms |
12404 KB |
Output is correct |
15 |
Correct |
123 ms |
12372 KB |
Output is correct |
16 |
Correct |
83 ms |
13008 KB |
Output is correct |
17 |
Correct |
126 ms |
12292 KB |
Output is correct |
18 |
Correct |
171 ms |
19912 KB |
Output is correct |
19 |
Correct |
118 ms |
12504 KB |
Output is correct |
20 |
Correct |
137 ms |
12344 KB |
Output is correct |