#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> cp;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vii;
typedef vector<cp> vcp;
typedef vector<ld> vld;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i,l,r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i,r,l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) (64 - __builtin_clzll(1ll*(x)) - 1)
#define numBit(x) (__builtin_popcountll(1ll*(x)))
#define getBit(x,i) ((x)>>(i)&1)
#define Pi acos(-1.0l)
#define sz(x) int(x.size())
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(x) cerr << #x << " = " << x << '\n';
const int N = 2e5+7;
int n,ans=N;
int cnt[N];
vi edge[N];
set<int> sA,sB;
void pre_dfs(int u, int par=-1) {
cnt[u]=1;
for(auto v:edge[u]) if(v!=par) {
pre_dfs(v,u);
cnt[u]+=cnt[v];
}
}
void upd(int x, int y, int z) {
ans=min(ans,max({x,y,z})-min({x,y,z}));
}
void process(int u) {
auto it=sA.upper_bound((n+cnt[u])/2);
if(it!=sA.end()) upd(cnt[u],n-*it,*it-cnt[u]);
if(it!=sA.begin()) {
it--;
upd(cnt[u],n-*it,*it-cnt[u]);
}
it=sB.upper_bound((n-cnt[u])/2);
if(it!=sB.end()) upd(cnt[u],n-*it-cnt[u],*it);
if(it!=sB.begin()) {
it--;
upd(cnt[u],n-*it-cnt[u],*it);
}
}
void dfs(int u, int par=-1) {
process(u);
sA.insert(cnt[u]);
for(auto v:edge[u]) if(v!=par) dfs(v,u);
sA.erase(cnt[u]);
sB.insert(cnt[u]);
}
int main() {
fastIO;
#ifndef ONLINE_JUDGE
//freopen("test.inp","r",stdin);
//freopen("test.out","w",stdout);
#endif // ONLINE_JUDGE
cin >> n;
forw(i,1,n) {
int u,v; cin >> u >> v;
u--, v--;
edge[u].pb(v);
edge[v].pb(u);
}
pre_dfs(0);
dfs(0);
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 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 |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 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 |
4940 KB |
Output is correct |
6 |
Correct |
5 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 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 |
4940 KB |
Output is correct |
6 |
Correct |
5 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5032 KB |
Output is correct |
11 |
Correct |
300 ms |
14716 KB |
Output is correct |
12 |
Correct |
324 ms |
14640 KB |
Output is correct |
13 |
Correct |
260 ms |
15004 KB |
Output is correct |
14 |
Correct |
249 ms |
15012 KB |
Output is correct |
15 |
Correct |
255 ms |
14592 KB |
Output is correct |
16 |
Correct |
159 ms |
14600 KB |
Output is correct |
17 |
Correct |
274 ms |
14728 KB |
Output is correct |
18 |
Correct |
248 ms |
21664 KB |
Output is correct |
19 |
Correct |
272 ms |
14756 KB |
Output is correct |
20 |
Correct |
221 ms |
14736 KB |
Output is correct |