#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _322ZVFH ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;
int main(){
_322ZVFH;
int n;
cin>>n;
vec(vi) adj(n);
rep(i,n-1){
int u,v;
cin>>u>>v;
u--,v--;
adj[u].pb(v);
adj[v].pb(u);
}
assert(n<=2000);
vi chs(n,0);
auto dfs=[&](auto self,int v,int par)->void{
chs[v]=1;
for(auto u : adj[v]){
if(u!=par){
self(self,u,v);
chs[v]+=chs[u];
}
}
};
dfs(dfs,0,-1);
multiset<int> up,lo;
int ans=1e9;
auto g=[&](int _a,int _b,int _c){
ans=min(ans,max({_a,_b,_c})-min({_a,_b,_c}));
};
auto f=[&](int v){
int l=chs[v];
vi rbts={(n+l)/2,(n-l)/2};
rep(i,2){
int rabbit=rbts[i];
auto it=(i==0?up.lower_bound(rabbit):lo.lower_bound(rabbit));
auto it1=(i==0?up.lower_bound(rabbit):lo.lower_bound(rabbit));
auto it2=(i==0?up.lower_bound(rabbit):lo.lower_bound(rabbit));
vi candies;
if((i==0?it!=up.end():it!=lo.end())){
candies.pb(*it);
}
if((i==0?it1!=up.end():it1!=lo.end())){
it1=next(it1);
candies.pb(*it1);
}
if((i==0?it2!=up.begin():it2!=lo.begin())){
it2=prev(it2);
candies.pb(*it2);
}
for(auto x : candies){
if(x==chs[0]) continue;
if(i==0) g(x-l,l,n-x);
else g(x,l,n-x-l);
}
}
};
auto dfs1=[&](auto self,int v,int par)->void{
f(v);
up.insert(chs[v]);
for(auto u : adj[v]){
if(u!=par){
self(self,u,v);
}
}
lo.insert(chs[v]);
if(up.find(chs[v])!=up.end()) up.erase(up.find(chs[v]));
};
dfs1(dfs1,0,-1);
cout<<ans<<"\n";
//
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Runtime error |
86 ms |
23064 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |