//#include"holiday.h"
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define REB(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;///lon
const int INF = 1e9 + 7;
const ll BASE = 137;
const int szBL = 450;
int n, m;
vector<int> ke[N];
int szC[N];
set<int> S[N];
int res = INF;
int Find (int sz1, int rem, set<int> &cur) {
int res = INF;
auto it = cur.lower_bound(rem / 2);
if (it != cur.end()) {
int sz2 = *it, sz3 = rem - sz2;
// cout << sz1 <<" "<<sz2 <<" "<<sz3 <<"\n";
res = min(res, max({sz1, sz2, sz3}) - min({sz1, sz2, sz3}));
}
if (it != cur.begin()) {
int sz2 = *prev(it), sz3 = rem - sz2;
res = min(res, max({sz1, sz2, sz3}) - min({sz1, sz2, sz3}));
}
return res;
}
void dfs (int u, int p) {
szC[u] = 1;
iter (&v, ke[u]) {
if (v != p) {
dfs(v, u);
szC[u] += szC[v];
if (SZ(S[u]) < SZ(S[v]))
swap(S[u], S[v]);
iter (&id, S[v]) {
res = min(res, Find(id, n - id, S[u]));
}
iter (&id, S[v]) {
S[u].insert(id);
}
set<int> ().swap(S[v]);
}
}
S[u].insert(szC[u]);
if (p != 0) {
res = min(res, Find(n - szC[u], szC[u], S[u]));
}
}
void solution () {
cin >> n;
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
dfs (1, 0);
cout << res <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +33
rep (i, 0, n) {
rep (j, 0, i) {
if (j == 0 || j == i) {
C[j][i] = 1;
continue;
}
C[j][i] = (C[j][i - 1] + C[j - 1][i - 1]) % Mod;
}
}
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |