#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 200'010;
const int lg = 20;
vector<int> A[N];
int sub[N];
int anc[N][lg];
int height[N];
int n;
int ans[N];
void dfs1(int v, int p, int h)
{
sub[v] = 1;
height[v] = h;
for (int u : A[v]) {
if (u == p)
continue;
dfs1(u, v, h+1);
sub[v] += sub[u];
}
}
void dfs2(int v, int p, int h)
{
sub[v] = 1;
height[v] = h;
anc[v][0] = p;
Loop (i,0,lg-1)
anc[v][i+1] = anc[anc[v][i]][i];
for (int u : A[v]) {
if (u == p)
continue;
dfs2(u, v, h+1);
sub[v] += sub[u];
}
}
int lca(int v, int u)
{
if (height[v] < height[u])
swap(v, u);
int diff = height[v] - height[u];
Loop (i,0,lg)
if ((diff>>i)&1)
v = anc[v][i];
if (v == u)
return v;
LoopR (i,0,lg) {
if (anc[v][i] != anc[u][i]) {
v = anc[v][i];
u = anc[u][i];
}
}
return anc[v][0];
}
int dis(int v, int u)
{
return height[v] + height[u] - 2 * height[lca(v, u)];
}
int centroid(int v, int p)
{
for (int u : A[v]) {
if (u != p && sub[u] * 2 > n)
return centroid(u, v);
}
return v;
}
void solve(int rt)
{
int v = rt, u = rt;
priority_queue<pair<int, int>> pq;
Loop (i,0,n)
pq.emplace(sub[i], i);
int d = 0;
while (pq.size()) {
auto [sz, w] = pq.top();
pq.pop();
int wv = dis(w, v);
int wu = dis(w, u);
if (wv > d) {
d = wv;
u = w;
} else if (wu > d) {
v = w;
d = wu;
}
ans[sz] = max(ans[sz], d + 1);
}
LoopR (i,0,n)
ans[i] = max(ans[i], ans[i+1]);
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n;
Loop (i,1,n) {
int v, u;
cin >> v >> u;
--v; --u;
A[v].push_back(u);
A[u].push_back(v);
}
dfs1(0, 0, 0);
int rt = centroid(0, -1);
dfs2(rt, rt, 0);
solve(rt);
Loop (i,1,n+1) {
if (i%2 == 1)
cout << "1\n";
else
cout << ans[i/2] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8376 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8468 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8376 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8468 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
4 ms |
8796 KB |
Output is correct |
23 |
Correct |
6 ms |
8856 KB |
Output is correct |
24 |
Correct |
6 ms |
8792 KB |
Output is correct |
25 |
Correct |
3 ms |
8796 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
4 ms |
8716 KB |
Output is correct |
28 |
Correct |
4 ms |
8876 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
4 ms |
8796 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
4 ms |
9052 KB |
Output is correct |
34 |
Correct |
4 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8796 KB |
Output is correct |
36 |
Correct |
4 ms |
8796 KB |
Output is correct |
37 |
Correct |
3 ms |
8796 KB |
Output is correct |
38 |
Correct |
3 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8376 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8468 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
4 ms |
8796 KB |
Output is correct |
23 |
Correct |
6 ms |
8856 KB |
Output is correct |
24 |
Correct |
6 ms |
8792 KB |
Output is correct |
25 |
Correct |
3 ms |
8796 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
4 ms |
8716 KB |
Output is correct |
28 |
Correct |
4 ms |
8876 KB |
Output is correct |
29 |
Correct |
4 ms |
8796 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
4 ms |
8796 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
4 ms |
9052 KB |
Output is correct |
34 |
Correct |
4 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8796 KB |
Output is correct |
36 |
Correct |
4 ms |
8796 KB |
Output is correct |
37 |
Correct |
3 ms |
8796 KB |
Output is correct |
38 |
Correct |
3 ms |
8796 KB |
Output is correct |
39 |
Correct |
150 ms |
33996 KB |
Output is correct |
40 |
Correct |
132 ms |
33744 KB |
Output is correct |
41 |
Correct |
140 ms |
33996 KB |
Output is correct |
42 |
Correct |
134 ms |
33940 KB |
Output is correct |
43 |
Correct |
126 ms |
34004 KB |
Output is correct |
44 |
Correct |
139 ms |
34168 KB |
Output is correct |
45 |
Correct |
198 ms |
38428 KB |
Output is correct |
46 |
Correct |
169 ms |
42696 KB |
Output is correct |
47 |
Correct |
109 ms |
34216 KB |
Output is correct |
48 |
Correct |
90 ms |
34636 KB |
Output is correct |
49 |
Correct |
144 ms |
34484 KB |
Output is correct |
50 |
Correct |
121 ms |
34660 KB |
Output is correct |
51 |
Correct |
129 ms |
41800 KB |
Output is correct |