#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;
set<int> A, B;
void dfs(int at, int p) {
for (int y: A) {
lo = min(lo, get({siz[at],siz[y]-siz[at],n-siz[y]}));
}
for (int y: B) {
lo = min(lo, get({siz[at],siz[y],n-siz[at]-siz[y]}));
}
A.insert(at);
for (int to: g[at]) {
if (to == p) continue;
dfs(to, at);
}
A.erase(at);
B.insert(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 |
5 ms |
5228 KB |
Output is correct |
3 |
Correct |
5 ms |
5228 KB |
Output is correct |
4 |
Correct |
5 ms |
5228 KB |
Output is correct |
5 |
Correct |
5 ms |
5228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5228 KB |
Output is correct |
2 |
Correct |
5 ms |
5228 KB |
Output is correct |
3 |
Correct |
5 ms |
5228 KB |
Output is correct |
4 |
Correct |
5 ms |
5228 KB |
Output is correct |
5 |
Correct |
5 ms |
5228 KB |
Output is correct |
6 |
Correct |
90 ms |
5484 KB |
Output is correct |
7 |
Correct |
92 ms |
5484 KB |
Output is correct |
8 |
Correct |
86 ms |
5484 KB |
Output is correct |
9 |
Correct |
87 ms |
5484 KB |
Output is correct |
10 |
Correct |
92 ms |
5484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5228 KB |
Output is correct |
2 |
Correct |
5 ms |
5228 KB |
Output is correct |
3 |
Correct |
5 ms |
5228 KB |
Output is correct |
4 |
Correct |
5 ms |
5228 KB |
Output is correct |
5 |
Correct |
5 ms |
5228 KB |
Output is correct |
6 |
Correct |
90 ms |
5484 KB |
Output is correct |
7 |
Correct |
92 ms |
5484 KB |
Output is correct |
8 |
Correct |
86 ms |
5484 KB |
Output is correct |
9 |
Correct |
87 ms |
5484 KB |
Output is correct |
10 |
Correct |
92 ms |
5484 KB |
Output is correct |
11 |
Execution timed out |
1097 ms |
29292 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |