#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) {
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;
int curK = 0, res = 0;
int dfs(int s, int p) {
int f = -INF;
if(sz[s] >= curK) f = 0;
int mx0 = -INF, mx1 = -INF;
for(int to : g[s]) {
if(to == p) continue;
int x = dfs(to, s);
if(x >= mx0) mx1 = mx0, mx0 = x;
else if(x > mx1) mx1 = x;
f = max(f, x + 1);
}
res = max(res, mx0 + mx1 + 2 + (s != root));
return f;
}
int func(int u, int v) {
if(sz[u] < sz[v]) swap(u, v);
return min(sz[v], n - sz[v]);
}
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);
}
precalc(1, 1);
if(n == 1) {
cout << 1;
return 0;
}
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;
}
}
// cout << x0 << ' ' << x1 << '\n';
vector<int> rs(n + 1, 1);
int k = 2 * 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);
for(int i = 1; i <= k; i++) {
if(i % 2) continue;
res = 0, curK = i / 2;
dfs(root, root);
rs[i] = res;
}
for(int i = 1; i <= n; i++)
cout << rs[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7324 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
3 ms |
7388 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7384 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7376 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7388 KB |
Output is correct |
19 |
Correct |
3 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7324 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
3 ms |
7388 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7384 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7376 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7388 KB |
Output is correct |
19 |
Correct |
3 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
84 ms |
8088 KB |
Output is correct |
23 |
Correct |
69 ms |
7972 KB |
Output is correct |
24 |
Correct |
71 ms |
7980 KB |
Output is correct |
25 |
Correct |
78 ms |
7904 KB |
Output is correct |
26 |
Correct |
69 ms |
7972 KB |
Output is correct |
27 |
Correct |
42 ms |
7996 KB |
Output is correct |
28 |
Correct |
93 ms |
8028 KB |
Output is correct |
29 |
Correct |
58 ms |
7980 KB |
Output is correct |
30 |
Correct |
82 ms |
7992 KB |
Output is correct |
31 |
Correct |
51 ms |
7980 KB |
Output is correct |
32 |
Correct |
86 ms |
8092 KB |
Output is correct |
33 |
Correct |
96 ms |
8176 KB |
Output is correct |
34 |
Correct |
6 ms |
7892 KB |
Output is correct |
35 |
Correct |
5 ms |
7904 KB |
Output is correct |
36 |
Correct |
68 ms |
8012 KB |
Output is correct |
37 |
Correct |
45 ms |
8000 KB |
Output is correct |
38 |
Correct |
68 ms |
8068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
3 ms |
7324 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
3 ms |
7388 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7384 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
3 ms |
7376 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7388 KB |
Output is correct |
19 |
Correct |
3 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
84 ms |
8088 KB |
Output is correct |
23 |
Correct |
69 ms |
7972 KB |
Output is correct |
24 |
Correct |
71 ms |
7980 KB |
Output is correct |
25 |
Correct |
78 ms |
7904 KB |
Output is correct |
26 |
Correct |
69 ms |
7972 KB |
Output is correct |
27 |
Correct |
42 ms |
7996 KB |
Output is correct |
28 |
Correct |
93 ms |
8028 KB |
Output is correct |
29 |
Correct |
58 ms |
7980 KB |
Output is correct |
30 |
Correct |
82 ms |
7992 KB |
Output is correct |
31 |
Correct |
51 ms |
7980 KB |
Output is correct |
32 |
Correct |
86 ms |
8092 KB |
Output is correct |
33 |
Correct |
96 ms |
8176 KB |
Output is correct |
34 |
Correct |
6 ms |
7892 KB |
Output is correct |
35 |
Correct |
5 ms |
7904 KB |
Output is correct |
36 |
Correct |
68 ms |
8012 KB |
Output is correct |
37 |
Correct |
45 ms |
8000 KB |
Output is correct |
38 |
Correct |
68 ms |
8068 KB |
Output is correct |
39 |
Execution timed out |
4059 ms |
36944 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |