#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int n,ans = 1e9;
vector<int> g[200001];
int kids[200001];
multiset<int> si,so;
void DFS(int u,int p = -1){
kids[u] = 1;
for(auto v : g[u]) if(v != p){
DFS(v,u);
kids[u] += kids[v];
}
}
inline void calc(int x,int y,int z){
ans = min(ans,max({abs(x-y),abs(y-z),abs(x-z)}));
}
void solve(int u,int p = -1){
/// y nam trong x
auto in = si.lower_bound((n - kids[u])/2);
if(in != si.end()){
calc(*in,n-kids[u]-(*in),kids[u]);
}
if(in != si.begin()){
auto pin = prev(in);
calc(*pin,n-kids[u]-(*pin),kids[u]);
}
/// y nam ngoai x
auto out = so.lower_bound((n - kids[u])/2);
if(out != so.end()){
calc(*out,n-kids[u]-(*out),kids[u]);
}
if(out != so.begin()){
auto pout = prev(out);
calc(*pout,n-kids[u]-(*pout),kids[u]);
}
si.insert(n - kids[u]);
for(auto v : g[u]) if(v != p){
solve(v,u);
}
so.insert(kids[u]);
si.erase(si.find(n - kids[u]));
}
int main(int argc,const char** argv){
cin >> n;
for(int i = 1;i < n;i++){
int x,y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
DFS(1);
solve(1);
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4932 KB |
Output is correct |
6 |
Correct |
5 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5068 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5136 KB |
Output is correct |
10 |
Correct |
5 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4932 KB |
Output is correct |
6 |
Correct |
5 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5068 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5136 KB |
Output is correct |
10 |
Correct |
5 ms |
5068 KB |
Output is correct |
11 |
Correct |
346 ms |
23956 KB |
Output is correct |
12 |
Correct |
367 ms |
23992 KB |
Output is correct |
13 |
Correct |
319 ms |
24412 KB |
Output is correct |
14 |
Correct |
342 ms |
24184 KB |
Output is correct |
15 |
Correct |
391 ms |
24056 KB |
Output is correct |
16 |
Correct |
264 ms |
23872 KB |
Output is correct |
17 |
Correct |
341 ms |
24112 KB |
Output is correct |
18 |
Correct |
351 ms |
29288 KB |
Output is correct |
19 |
Correct |
319 ms |
24180 KB |
Output is correct |
20 |
Correct |
341 ms |
24064 KB |
Output is correct |