#include <bits/stdc++.h>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 2e5 + 7;
vi G[N];
int sz[N], n;
void get_c(int u, int& c, int p = 0) {
int mx = 0;
sz[u] = 1;
for(int v:G[u]) if(v != p) {
get_c(v, c, u);
sz[u] += sz[v];
mx = max(mx, sz[v]);
}
mx = max(mx, n - sz[u]);
if(mx * 2 <= n) c = u;
}
V<pi> aux;
int p[N][20], d[N];
void dfs(int u, int pa = 0) {
sz[u] = 1;
p[u][0] = pa;
for(int i = 1; i < 20; i++)
p[u][i] = p[p[u][i - 1]][i - 1];
for(int v:G[u]) if(v != pa) {
d[v] = d[u] + 1;
dfs(v, u);
sz[u] += sz[v];
}
aux.EB(sz[u], u);
}
int lca(int u, int v) {
if(d[u] > d[v]) swap(u, v);
for(int i = 19; i >= 0; i--)
if((d[v] - d[u]) >> i & 1)
v = p[v][i];
if(u == v) return u;
for(int i = 19; i >= 0; i--)
if(p[u][i] != p[v][i])
u = p[u][i], v = p[v][i];
return p[u][0];
}
int dist(int u, int v) {
return d[u] + d[v] - d[lca(u, v)] * 2;
}
int ans[N];
signed main()
{
IO_OP;
cin >> n;
for(int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
G[u].PB(v);
G[v].PB(u);
}
int c; get_c(1, c);
dfs(c);
sort(ALL(aux));
int a = -1, b = -1;
for(int i = n / 2 * 2; i >= 2; i -= 2) {
while(SZ(aux) && aux.back().F >= i / 2) {
int u = aux.back().S;
aux.pop_back();
if(a == -1) a = u;
else if(b == -1) b = u;
else {
if(dist(a, u) > dist(a, b) || dist(b, u) > dist(a, b)) {
if(dist(a, u) > dist(b, u)) b = u;
else a = u;
}
}
}
if(a != -1 && b != -1) ans[i] = dist(a, b);
}
for(int i = 1; i <= n; i++)
cout << ans[i] + 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
4948 KB |
Output is correct |
22 |
Correct |
6 ms |
5484 KB |
Output is correct |
23 |
Correct |
7 ms |
5588 KB |
Output is correct |
24 |
Correct |
6 ms |
5588 KB |
Output is correct |
25 |
Correct |
6 ms |
5548 KB |
Output is correct |
26 |
Correct |
6 ms |
5552 KB |
Output is correct |
27 |
Correct |
6 ms |
5588 KB |
Output is correct |
28 |
Correct |
6 ms |
5548 KB |
Output is correct |
29 |
Correct |
6 ms |
5588 KB |
Output is correct |
30 |
Correct |
6 ms |
5588 KB |
Output is correct |
31 |
Correct |
6 ms |
5588 KB |
Output is correct |
32 |
Correct |
8 ms |
5808 KB |
Output is correct |
33 |
Correct |
6 ms |
5844 KB |
Output is correct |
34 |
Correct |
6 ms |
5544 KB |
Output is correct |
35 |
Correct |
6 ms |
5556 KB |
Output is correct |
36 |
Correct |
6 ms |
5552 KB |
Output is correct |
37 |
Correct |
6 ms |
5540 KB |
Output is correct |
38 |
Correct |
6 ms |
5676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
4948 KB |
Output is correct |
22 |
Correct |
6 ms |
5484 KB |
Output is correct |
23 |
Correct |
7 ms |
5588 KB |
Output is correct |
24 |
Correct |
6 ms |
5588 KB |
Output is correct |
25 |
Correct |
6 ms |
5548 KB |
Output is correct |
26 |
Correct |
6 ms |
5552 KB |
Output is correct |
27 |
Correct |
6 ms |
5588 KB |
Output is correct |
28 |
Correct |
6 ms |
5548 KB |
Output is correct |
29 |
Correct |
6 ms |
5588 KB |
Output is correct |
30 |
Correct |
6 ms |
5588 KB |
Output is correct |
31 |
Correct |
6 ms |
5588 KB |
Output is correct |
32 |
Correct |
8 ms |
5808 KB |
Output is correct |
33 |
Correct |
6 ms |
5844 KB |
Output is correct |
34 |
Correct |
6 ms |
5544 KB |
Output is correct |
35 |
Correct |
6 ms |
5556 KB |
Output is correct |
36 |
Correct |
6 ms |
5552 KB |
Output is correct |
37 |
Correct |
6 ms |
5540 KB |
Output is correct |
38 |
Correct |
6 ms |
5676 KB |
Output is correct |
39 |
Correct |
260 ms |
31120 KB |
Output is correct |
40 |
Correct |
257 ms |
30584 KB |
Output is correct |
41 |
Correct |
261 ms |
31108 KB |
Output is correct |
42 |
Correct |
253 ms |
31540 KB |
Output is correct |
43 |
Correct |
271 ms |
31520 KB |
Output is correct |
44 |
Correct |
264 ms |
31640 KB |
Output is correct |
45 |
Correct |
395 ms |
37372 KB |
Output is correct |
46 |
Correct |
292 ms |
42296 KB |
Output is correct |
47 |
Correct |
210 ms |
31264 KB |
Output is correct |
48 |
Correct |
171 ms |
31488 KB |
Output is correct |
49 |
Correct |
263 ms |
32264 KB |
Output is correct |
50 |
Correct |
185 ms |
32180 KB |
Output is correct |
51 |
Correct |
236 ms |
41112 KB |
Output is correct |