#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
ll n;
ll calcval(ll a, ll b){
ll c = n-a-b;
return max({abs(a-b), abs(a-c), abs(b-c)});
}
ll ans = 1e18;
vector<ll> g[200009];
multiset<ll> s, ins;
ll sz[200009];
int uptop = 0;
void dfs1(ll v, ll prt){
sz[v] = 1;
for(auto u : g[v])
if(u != prt){
dfs1(u, v);
sz[v] += sz[u];
}
s.insert(sz[v]);
}
void calc(ll x, multiset<ll> &s){
ll opt = (n-x)/2;
auto it = s.lower_bound(opt);
if(it != s.end()){
ans = min(ans, calcval(x, *it));
}
if(it != s.begin()){
--it;
ans = min(ans, calcval(x, *it));
}
}
void clean(ll v, ll prt, ll type){
if(type == 1){
s.insert(sz[v]);
ins.erase(ins.lower_bound(sz[v]));
}
else{
s.erase(s.lower_bound(sz[v]));
ins.insert(sz[v]);
}
for(auto u : g[v])
if(u != prt)
clean(u, v, type);
}
void dfs2(ll v, ll prt, ll del){
s.erase(s.lower_bound(sz[v]));
ins.insert(sz[v]);
pii maxi = {0, 0};
for(auto u : g[v])
if(u != prt)
maxi = max(maxi, {sz[u], u});
for(auto u : g[v])
if(u != prt && u != maxi.ss)
dfs2(u, v, 0);
if(maxi.ss)
dfs2(maxi.ss, v, 1);
for(auto u : g[v])
if(u != prt && u != maxi.ss)
clean(u, v, 0);
calc(sz[v], s);
calc(n-sz[v], ins);
if(del == 0){
clean(v, prt, 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n;
for(ll i = 0; i < n-1; ++i){
ll x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
dfs1(1, 0);
dfs2(1, 0, 0);
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
8 ms |
5228 KB |
Output is correct |
7 |
Correct |
8 ms |
5228 KB |
Output is correct |
8 |
Correct |
7 ms |
5228 KB |
Output is correct |
9 |
Correct |
7 ms |
5228 KB |
Output is correct |
10 |
Correct |
8 ms |
5228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
8 ms |
5228 KB |
Output is correct |
7 |
Correct |
8 ms |
5228 KB |
Output is correct |
8 |
Correct |
7 ms |
5228 KB |
Output is correct |
9 |
Correct |
7 ms |
5228 KB |
Output is correct |
10 |
Correct |
8 ms |
5228 KB |
Output is correct |
11 |
Execution timed out |
1084 ms |
25968 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |