#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
const int maxn = 2e5 + 10;
const int LOG = 20;
int n;
vector<int> g[maxn];
int par[LOG+1][maxn];
int dep[maxn];
int siz[maxn];
void hack(int at, int p=0) {
for (int i=1; i<LOG; i++) {
par[i][at] = par[i-1][par[i-1][at]];
}
siz[at] = 1;
for (int to: g[at]) {
if (to==p) continue;
dep[to]=1+dep[at];
par[0][to]=at;
hack(to, at);
siz[at] += siz[to];
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u,v);
int dx = dep[u]-dep[v];
for (int i=0; i<LOG; i++) {
if (dx>>i&1) {
u = par[i][u];
}
}
if (u==v) return u;
for (int j=LOG-1; j>=0; j--) {
if (par[j][u]!=par[j][v]) {
u = par[j][u];
v = par[j][v];
}
}
return par[0][u];
}
int get(vector<int> a) {
sort(a.begin(),a.end());
return a[2]-a[0];
}
int lo;
vector<int> vfind(multiset<int> &s, int x) {
if (s.empty()) return {};
auto iter = s.lower_bound(x);
if (iter == s.end()) --iter;
vector<int> res;
res.push_back(*iter);
auto piter = iter;
//fwd
for (int it=0; it<2; it++) {
++iter;
if (iter==s.end()) break;
res.push_back(*iter);
}
iter = piter;
//prv
for (int it=0; it<2; it++) {
if (iter==s.begin()) break;
--iter;
res.push_back(*iter);
}
return res;
}
multiset<int> A, B;
vector<int> solveA(int x) {
vector<int> res;
auto tmp = vfind(A, (n+x)/2);
res.insert(res.end(), tmp.begin(), tmp.end());
tmp = vfind(A, n-x);
res.insert(res.end(), tmp.begin(), tmp.end());
tmp = vfind(A, 2*x);
res.insert(res.end(), tmp.begin(), tmp.end());
return res;
}
vector<int> solveB(int x) {
vector<int> res;
auto tmp = vfind(B, (n-x)/2);
res.insert(res.end(), tmp.begin(), tmp.end());
tmp = vfind(B, x);
res.insert(res.end(), tmp.begin(), tmp.end());
tmp = vfind(B, n-2*x);
res.insert(res.end(), tmp.begin(), tmp.end());
return res;
}
void dfs(int at, int p) {
for (int ysiz: solveA(siz[at])) {
int cur = get({siz[at], ysiz-siz[at], n-ysiz});
lo = min(lo, cur);
}
for (int ysiz: solveB(siz[at])) {
int cur = get({siz[at], ysiz, n-siz[at]-ysiz});
lo = min(lo, cur);
}
A.insert(siz[at]);
for (int to: g[at]) {
if (to == p) continue;
dfs(to, at);
}
A.erase(A.find(siz[at]));
B.insert(siz[at]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
for (int i=0; i<n-1; i++) {
int u,v; cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
assert(n>=3);
hack(1);
lo = n;
dfs(1,0);
cout<<lo<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5228 KB |
Output is correct |
2 |
Correct |
4 ms |
5228 KB |
Output is correct |
3 |
Correct |
4 ms |
5228 KB |
Output is correct |
4 |
Correct |
4 ms |
5228 KB |
Output is correct |
5 |
Correct |
4 ms |
5228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5228 KB |
Output is correct |
2 |
Correct |
4 ms |
5228 KB |
Output is correct |
3 |
Correct |
4 ms |
5228 KB |
Output is correct |
4 |
Correct |
4 ms |
5228 KB |
Output is correct |
5 |
Correct |
4 ms |
5228 KB |
Output is correct |
6 |
Correct |
9 ms |
5484 KB |
Output is correct |
7 |
Correct |
10 ms |
5484 KB |
Output is correct |
8 |
Correct |
8 ms |
5484 KB |
Output is correct |
9 |
Correct |
9 ms |
5612 KB |
Output is correct |
10 |
Correct |
9 ms |
5484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5228 KB |
Output is correct |
2 |
Correct |
4 ms |
5228 KB |
Output is correct |
3 |
Correct |
4 ms |
5228 KB |
Output is correct |
4 |
Correct |
4 ms |
5228 KB |
Output is correct |
5 |
Correct |
4 ms |
5228 KB |
Output is correct |
6 |
Correct |
9 ms |
5484 KB |
Output is correct |
7 |
Correct |
10 ms |
5484 KB |
Output is correct |
8 |
Correct |
8 ms |
5484 KB |
Output is correct |
9 |
Correct |
9 ms |
5612 KB |
Output is correct |
10 |
Correct |
9 ms |
5484 KB |
Output is correct |
11 |
Correct |
796 ms |
38508 KB |
Output is correct |
12 |
Correct |
897 ms |
38124 KB |
Output is correct |
13 |
Correct |
685 ms |
41188 KB |
Output is correct |
14 |
Correct |
720 ms |
40812 KB |
Output is correct |
15 |
Correct |
874 ms |
40532 KB |
Output is correct |
16 |
Correct |
612 ms |
40424 KB |
Output is correct |
17 |
Correct |
772 ms |
40684 KB |
Output is correct |
18 |
Correct |
807 ms |
47712 KB |
Output is correct |
19 |
Correct |
735 ms |
40684 KB |
Output is correct |
20 |
Correct |
841 ms |
40604 KB |
Output is correct |