#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);
}
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]);
up.erase(up.find(chs[v]));
};
dfs1(dfs1,0,-1);
cout<<ans<<"\n";
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
216 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 |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
216 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 |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
244 ms |
21564 KB |
Output is correct |
12 |
Correct |
281 ms |
23972 KB |
Output is correct |
13 |
Correct |
211 ms |
24388 KB |
Output is correct |
14 |
Correct |
216 ms |
24168 KB |
Output is correct |
15 |
Correct |
254 ms |
23964 KB |
Output is correct |
16 |
Correct |
187 ms |
23976 KB |
Output is correct |
17 |
Correct |
233 ms |
24104 KB |
Output is correct |
18 |
Correct |
252 ms |
27128 KB |
Output is correct |
19 |
Correct |
222 ms |
24132 KB |
Output is correct |
20 |
Correct |
244 ms |
23960 KB |
Output is correct |