#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5 + 10;
const int INF = 1e9;
vector<int> g[N];
int n;
int sz[N];
int anc[N][20];
int tin[N], tout[N], T, depth[N];
void precalc(int s, int p) {
if(s == p) T = 0, depth[s] = 0;
sz[s] = 1;
anc[s][0] = p;
tin[s] = T++;
for(int i = 1; i < 20; i++)
anc[s][i] = anc[anc[s][i - 1]][i - 1];
for(int to : g[s]) {
if(to == p) continue;
depth[to] = depth[s] + 1;
precalc(to, s);
sz[s] += sz[to];
}
tout[s] = T++;
}
bool up(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
if(up(u, v)) return u;
if(up(v, u)) return v;
for(int i = 19; i >= 0; i--) {
if(!up(anc[u][i], v)) u = anc[u][i];
}
return anc[u][0];
}
int dist(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
int root;
vector<int> rs(N, 1);
int LIM;
int func(int u, int v) {
if(sz[u] < sz[v]) swap(u, v);
return min(sz[v], n - sz[v]);
}
vector<int> ver[N];
int lb[N];
void DFS(int s, int p) {
lb[s] = 1;
for(int to : g[s]) {
if(to == p) continue;
DFS(to, s);
lb[s] = max(lb[to], lb[s]);
}
while(lb[s] <= min(sz[s], LIM)) {
ver[lb[s]++].push_back(s);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
vector<pair<int, int>> edges;
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
edges.push_back({a, b});
g[a].push_back(b);
g[b].push_back(a);
}
if(n == 1) {
cout << 1;
return 0;
}
precalc(1, 1);
int x0 = edges[0].first, x1 = edges[0].second;
for(auto [u, v] : edges) {
if(func(x0, x1) < func(u, v)) {
x0 = u;
x1 = v;
}
}
LIM = func(x0, x1);
g[x0].erase(find(g[x0].begin(), g[x0].end(), x1));
g[x1].erase(find(g[x1].begin(), g[x1].end(), x0));
root = n + 1;
g[root].push_back(x0);
g[root].push_back(x1);
precalc(root, root);
DFS(root, root);
for(int i = 1; i <= LIM; i++) {
if(ver[i].size() < 2) break;
int x = ver[i][0], w = 0;
for(int c : ver[i]) {
if(depth[c] > depth[x]) x = c;
}
for(int c : ver[i]) {
int v = dist(c, x) + (lca(c, x) != root);
w = max(w, v);
}
rs[2 * i] = w;
}
for(int i = 1; i <= n; i++)
cout << rs[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15584 KB |
Output is correct |
2 |
Correct |
8 ms |
15572 KB |
Output is correct |
3 |
Correct |
7 ms |
15616 KB |
Output is correct |
4 |
Correct |
8 ms |
15616 KB |
Output is correct |
5 |
Correct |
8 ms |
15740 KB |
Output is correct |
6 |
Correct |
8 ms |
15572 KB |
Output is correct |
7 |
Correct |
7 ms |
15572 KB |
Output is correct |
8 |
Correct |
7 ms |
15572 KB |
Output is correct |
9 |
Correct |
8 ms |
15620 KB |
Output is correct |
10 |
Correct |
7 ms |
15608 KB |
Output is correct |
11 |
Correct |
7 ms |
15612 KB |
Output is correct |
12 |
Correct |
7 ms |
15572 KB |
Output is correct |
13 |
Correct |
7 ms |
15616 KB |
Output is correct |
14 |
Correct |
7 ms |
15572 KB |
Output is correct |
15 |
Correct |
7 ms |
15572 KB |
Output is correct |
16 |
Correct |
8 ms |
15516 KB |
Output is correct |
17 |
Correct |
7 ms |
15572 KB |
Output is correct |
18 |
Correct |
7 ms |
15612 KB |
Output is correct |
19 |
Correct |
7 ms |
15572 KB |
Output is correct |
20 |
Correct |
10 ms |
15632 KB |
Output is correct |
21 |
Correct |
7 ms |
15548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15584 KB |
Output is correct |
2 |
Correct |
8 ms |
15572 KB |
Output is correct |
3 |
Correct |
7 ms |
15616 KB |
Output is correct |
4 |
Correct |
8 ms |
15616 KB |
Output is correct |
5 |
Correct |
8 ms |
15740 KB |
Output is correct |
6 |
Correct |
8 ms |
15572 KB |
Output is correct |
7 |
Correct |
7 ms |
15572 KB |
Output is correct |
8 |
Correct |
7 ms |
15572 KB |
Output is correct |
9 |
Correct |
8 ms |
15620 KB |
Output is correct |
10 |
Correct |
7 ms |
15608 KB |
Output is correct |
11 |
Correct |
7 ms |
15612 KB |
Output is correct |
12 |
Correct |
7 ms |
15572 KB |
Output is correct |
13 |
Correct |
7 ms |
15616 KB |
Output is correct |
14 |
Correct |
7 ms |
15572 KB |
Output is correct |
15 |
Correct |
7 ms |
15572 KB |
Output is correct |
16 |
Correct |
8 ms |
15516 KB |
Output is correct |
17 |
Correct |
7 ms |
15572 KB |
Output is correct |
18 |
Correct |
7 ms |
15612 KB |
Output is correct |
19 |
Correct |
7 ms |
15572 KB |
Output is correct |
20 |
Correct |
10 ms |
15632 KB |
Output is correct |
21 |
Correct |
7 ms |
15548 KB |
Output is correct |
22 |
Correct |
10 ms |
16284 KB |
Output is correct |
23 |
Correct |
11 ms |
16344 KB |
Output is correct |
24 |
Correct |
11 ms |
16268 KB |
Output is correct |
25 |
Correct |
11 ms |
16272 KB |
Output is correct |
26 |
Correct |
11 ms |
16252 KB |
Output is correct |
27 |
Correct |
11 ms |
16212 KB |
Output is correct |
28 |
Correct |
11 ms |
16268 KB |
Output is correct |
29 |
Correct |
11 ms |
16208 KB |
Output is correct |
30 |
Correct |
11 ms |
16276 KB |
Output is correct |
31 |
Correct |
12 ms |
16328 KB |
Output is correct |
32 |
Correct |
10 ms |
16340 KB |
Output is correct |
33 |
Correct |
10 ms |
16404 KB |
Output is correct |
34 |
Correct |
10 ms |
16212 KB |
Output is correct |
35 |
Correct |
10 ms |
16168 KB |
Output is correct |
36 |
Correct |
11 ms |
16340 KB |
Output is correct |
37 |
Correct |
13 ms |
16472 KB |
Output is correct |
38 |
Correct |
10 ms |
16280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15584 KB |
Output is correct |
2 |
Correct |
8 ms |
15572 KB |
Output is correct |
3 |
Correct |
7 ms |
15616 KB |
Output is correct |
4 |
Correct |
8 ms |
15616 KB |
Output is correct |
5 |
Correct |
8 ms |
15740 KB |
Output is correct |
6 |
Correct |
8 ms |
15572 KB |
Output is correct |
7 |
Correct |
7 ms |
15572 KB |
Output is correct |
8 |
Correct |
7 ms |
15572 KB |
Output is correct |
9 |
Correct |
8 ms |
15620 KB |
Output is correct |
10 |
Correct |
7 ms |
15608 KB |
Output is correct |
11 |
Correct |
7 ms |
15612 KB |
Output is correct |
12 |
Correct |
7 ms |
15572 KB |
Output is correct |
13 |
Correct |
7 ms |
15616 KB |
Output is correct |
14 |
Correct |
7 ms |
15572 KB |
Output is correct |
15 |
Correct |
7 ms |
15572 KB |
Output is correct |
16 |
Correct |
8 ms |
15516 KB |
Output is correct |
17 |
Correct |
7 ms |
15572 KB |
Output is correct |
18 |
Correct |
7 ms |
15612 KB |
Output is correct |
19 |
Correct |
7 ms |
15572 KB |
Output is correct |
20 |
Correct |
10 ms |
15632 KB |
Output is correct |
21 |
Correct |
7 ms |
15548 KB |
Output is correct |
22 |
Correct |
10 ms |
16284 KB |
Output is correct |
23 |
Correct |
11 ms |
16344 KB |
Output is correct |
24 |
Correct |
11 ms |
16268 KB |
Output is correct |
25 |
Correct |
11 ms |
16272 KB |
Output is correct |
26 |
Correct |
11 ms |
16252 KB |
Output is correct |
27 |
Correct |
11 ms |
16212 KB |
Output is correct |
28 |
Correct |
11 ms |
16268 KB |
Output is correct |
29 |
Correct |
11 ms |
16208 KB |
Output is correct |
30 |
Correct |
11 ms |
16276 KB |
Output is correct |
31 |
Correct |
12 ms |
16328 KB |
Output is correct |
32 |
Correct |
10 ms |
16340 KB |
Output is correct |
33 |
Correct |
10 ms |
16404 KB |
Output is correct |
34 |
Correct |
10 ms |
16212 KB |
Output is correct |
35 |
Correct |
10 ms |
16168 KB |
Output is correct |
36 |
Correct |
11 ms |
16340 KB |
Output is correct |
37 |
Correct |
13 ms |
16472 KB |
Output is correct |
38 |
Correct |
10 ms |
16280 KB |
Output is correct |
39 |
Correct |
355 ms |
52208 KB |
Output is correct |
40 |
Correct |
346 ms |
51448 KB |
Output is correct |
41 |
Correct |
438 ms |
52072 KB |
Output is correct |
42 |
Correct |
375 ms |
52780 KB |
Output is correct |
43 |
Correct |
362 ms |
52708 KB |
Output is correct |
44 |
Correct |
357 ms |
52404 KB |
Output is correct |
45 |
Correct |
349 ms |
54912 KB |
Output is correct |
46 |
Correct |
313 ms |
58272 KB |
Output is correct |
47 |
Correct |
227 ms |
48116 KB |
Output is correct |
48 |
Correct |
199 ms |
48440 KB |
Output is correct |
49 |
Correct |
289 ms |
51628 KB |
Output is correct |
50 |
Correct |
236 ms |
51232 KB |
Output is correct |
51 |
Correct |
264 ms |
58028 KB |
Output is correct |