#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) x.size())
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x, y) cerr << #x << " is " << x << ", " << #y << " is " << y << endl;
typedef long long lint;
typedef pair<lint, lint> ii;
int n;
int ans[200005];
vector<int> adj[200005];
bool cented[200005];
int sz[200005];
int dis[200005];
vector<int> nodes;
void dfs(int u, int p){
nodes.push_back(u);
sz[u] = 1;
for(int v : adj[u]){
if(cented[v] or v == p) continue;
dis[v] = dis[u]+1;
dfs(v, u);
sz[u] += sz[v];
}
}
void init(int u, int p = -1){
dis[u] = 0;
nodes.clear();
dfs(u,p);
}
struct thing{ int sub, dis, sz; };
void recurse(int R){
init(R, -1);
int cent = -1;
for(int u : nodes){
int maxsz = sz[R] - sz[u];
for(int v : adj[u]){
if(cented[v] or sz[v] > sz[u]) continue;
maxsz = max(maxsz, sz[v]);
}
if(maxsz <= sz(nodes)/2){
cent = u;
break;
}
}
vector<thing> stuff;
for(int v : adj[cent]){
if(cented[v]) continue;
init(v, cent);
for(int x : nodes){
stuff.push_back({v, dis[x], sz[x]});
}
}
sort(all(stuff), [&](thing &a, thing &b){ return a.sz > b.sz; });
ii b1 = {-1, -1}, b2 = {-2, -2};
for(thing &s : stuff){
///if root is the endpoint
int SZ = 2*min(s.sz, n-s.sz);
ans[SZ] = max(ans[SZ], s.dis+2);
//show2(s.sub, s.dis);
///if root is not endpoint
ii cur = ii(s.dis, s.sub);
if(b1.second == cur.second) b1 = max(b1, cur);
else b2 = max(b2, cur);
if(b1 < b2) swap(b1, b2);
assert(b1.second != b2.second);
ans[2*s.sz] = max((lint) ans[2*s.sz], b1.first + b2.first + 3);
}
cented[cent] = true;
for(int v : adj[cent]){
if(cented[v]) continue;
recurse(v);
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(int i = 1;i < n;i++){
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
fill(ans, ans+n+1, 1);
recurse(1);
int nn = n;
if(nn&1) nn--;
for(int i = nn;i >= 2;i -= 2) ans[i] = max(ans[i], ans[i+2]);
for(int i = 1;i <= n;i++) cout << ans[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4960 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4960 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5032 KB |
Output is correct |
22 |
Correct |
8 ms |
5324 KB |
Output is correct |
23 |
Correct |
8 ms |
5376 KB |
Output is correct |
24 |
Correct |
8 ms |
5324 KB |
Output is correct |
25 |
Correct |
8 ms |
5296 KB |
Output is correct |
26 |
Correct |
8 ms |
5388 KB |
Output is correct |
27 |
Correct |
8 ms |
5324 KB |
Output is correct |
28 |
Correct |
7 ms |
5384 KB |
Output is correct |
29 |
Correct |
7 ms |
5324 KB |
Output is correct |
30 |
Correct |
8 ms |
5324 KB |
Output is correct |
31 |
Correct |
8 ms |
5376 KB |
Output is correct |
32 |
Correct |
9 ms |
5492 KB |
Output is correct |
33 |
Correct |
10 ms |
5580 KB |
Output is correct |
34 |
Correct |
6 ms |
5300 KB |
Output is correct |
35 |
Correct |
5 ms |
5300 KB |
Output is correct |
36 |
Correct |
8 ms |
5324 KB |
Output is correct |
37 |
Correct |
6 ms |
5324 KB |
Output is correct |
38 |
Correct |
7 ms |
5452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4960 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5032 KB |
Output is correct |
22 |
Correct |
8 ms |
5324 KB |
Output is correct |
23 |
Correct |
8 ms |
5376 KB |
Output is correct |
24 |
Correct |
8 ms |
5324 KB |
Output is correct |
25 |
Correct |
8 ms |
5296 KB |
Output is correct |
26 |
Correct |
8 ms |
5388 KB |
Output is correct |
27 |
Correct |
8 ms |
5324 KB |
Output is correct |
28 |
Correct |
7 ms |
5384 KB |
Output is correct |
29 |
Correct |
7 ms |
5324 KB |
Output is correct |
30 |
Correct |
8 ms |
5324 KB |
Output is correct |
31 |
Correct |
8 ms |
5376 KB |
Output is correct |
32 |
Correct |
9 ms |
5492 KB |
Output is correct |
33 |
Correct |
10 ms |
5580 KB |
Output is correct |
34 |
Correct |
6 ms |
5300 KB |
Output is correct |
35 |
Correct |
5 ms |
5300 KB |
Output is correct |
36 |
Correct |
8 ms |
5324 KB |
Output is correct |
37 |
Correct |
6 ms |
5324 KB |
Output is correct |
38 |
Correct |
7 ms |
5452 KB |
Output is correct |
39 |
Correct |
526 ms |
21988 KB |
Output is correct |
40 |
Correct |
462 ms |
21656 KB |
Output is correct |
41 |
Correct |
503 ms |
22100 KB |
Output is correct |
42 |
Correct |
495 ms |
22244 KB |
Output is correct |
43 |
Correct |
488 ms |
22276 KB |
Output is correct |
44 |
Correct |
506 ms |
22156 KB |
Output is correct |
45 |
Correct |
840 ms |
26956 KB |
Output is correct |
46 |
Correct |
744 ms |
31060 KB |
Output is correct |
47 |
Correct |
205 ms |
21300 KB |
Output is correct |
48 |
Correct |
187 ms |
21400 KB |
Output is correct |
49 |
Correct |
491 ms |
23008 KB |
Output is correct |
50 |
Correct |
218 ms |
22892 KB |
Output is correct |
51 |
Correct |
429 ms |
30028 KB |
Output is correct |