#include <bits/stdc++.h>
#define loop(i, a, b) for(long long i=a;i<b;i++)
#define pool(i, a, b) for(long long i=a-1;i>=b;i--)
#define fore(i, a) for(auto&& i:a)
#define fi first
#define se second
#define ps(a) push_back(a)
#define pb(a) pop_back(a)
#define sc scanf
#define vc vector
#define pa pair<ll, ll>
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define llmax LLONG_MAX/2
#define llmin -LLONG_MAX/2
using namespace std;
#define mn 200100
#define pa pair<ll, ll>
#define ld long double
vc<ll> edg[mn];
ll n, siz[mn];
void get(ll u, ll p){
siz[u]=1;
fore(v, edg[u]) if(v!=p){
get(v, u);
siz[u]+=siz[v];
}
}
set<ll> alls, sub[mn];
ll raz(ll a, ll b, ll c){return max(a, max(b, c)) - min(a, min(b, c));}
ll ans;
void dfs(ll u, ll p){
auto z=alls.lower_bound((n-siz[u])/2);
if(z!=alls.end()) ans=min(ans, raz(siz[u], (*z), n-(*z)-siz[u]));
if(z!=alls.begin()){--z; ans=min(ans, raz(siz[u], (*z), n-(*z)-siz[u]));}
fore(v, edg[u]) if(v!=p){
dfs(v, u);
if(sub[v].size()>sub[u].size()) swap(sub[v], sub[u]);
}
fore(v, edg[u]) if(v!=p){
fore(el, sub[v]) sub[u].insert(el);
}
auto s=sub[u].lower_bound(siz[u]/2);
/*if(s!=sub[u].end()){
cout << n << " "<<siz[u]<< " "<<(*s)<<endl;
cout<<n-siz[u]<<" " <<siz[u] -(*s)<<" "<< (*s)<< " "<<raz(n-siz[u], siz[u] -(*s), (*s))<<endl;
}*/
if(s!=sub[u].end()) ans=min(ans, raz(n-siz[u], siz[u] -(*s), (*s)));
if(s!=sub[u].begin()){--s; ans=min(ans, raz(n-siz[u], siz[u] -(*s), (*s)));}
sub[u].insert(siz[u]);
alls.insert(siz[u]);
}
int main() {
cin >> n;
ans=raz(1, 1, n-2);
loop(i, 0, n-1){
ll a, b;cin >> a >> b;
edg[a].ps(b);
edg[b].ps(a);
}
get(1, -1);
dfs(1, -1);
cout << ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14572 KB |
Output is correct |
7 |
Correct |
11 ms |
14572 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
12 ms |
14572 KB |
Output is correct |
10 |
Correct |
12 ms |
14572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14572 KB |
Output is correct |
7 |
Correct |
11 ms |
14572 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
12 ms |
14572 KB |
Output is correct |
10 |
Correct |
12 ms |
14572 KB |
Output is correct |
11 |
Correct |
376 ms |
36844 KB |
Output is correct |
12 |
Correct |
395 ms |
37740 KB |
Output is correct |
13 |
Correct |
316 ms |
36716 KB |
Output is correct |
14 |
Correct |
331 ms |
37100 KB |
Output is correct |
15 |
Correct |
381 ms |
37740 KB |
Output is correct |
16 |
Correct |
262 ms |
35040 KB |
Output is correct |
17 |
Correct |
335 ms |
36844 KB |
Output is correct |
18 |
Correct |
329 ms |
42604 KB |
Output is correct |
19 |
Correct |
339 ms |
37996 KB |
Output is correct |
20 |
Correct |
364 ms |
37996 KB |
Output is correct |