#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXN = 2e5;
const int MAXP = 20;
vector<int> adj[MAXN];
int sz[MAXN], depth[MAXN], par[MAXP][MAXN];
void dfsSz(int u, int p) {
sz[u] = 1;
for (int v : adj[u])
if (v != p) {
dfsSz(v, u);
sz[u] += sz[v];
}
}
void dfs(int u, int p) {
par[0][u] = p;
for (int i = 0; i < MAXP - 1; ++i)
par[i + 1][u] = par[i][par[i][u]];
sz[u] = 1;
for (int v : adj[u])
if (v != p) {
depth[v] = depth[u] + 1;
dfs(v, u);
sz[u] += sz[v];
}
}
int getLca(int u, int v) {
if (depth[u] < depth[v])
swap(u, v);
int d = depth[u] - depth[v];
for (int p = 0; p < MAXP; ++p)
if ((1 << p) & d)
u = par[p][u];
if (u == v)
return u;
for (int p = MAXP - 1; p >= 0; --p) {
int s = par[p][u];
int t = par[p][v];
if (s != t) {
u = s;
v = t;
}
}
return par[0][u];
}
int dis(int u, int v) {
int lca = getLca(u, v);
return depth[u] + depth[v] - 2 * depth[lca];
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbSommet;
cin >> nbSommet;
for (int i = 1; i < nbSommet; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfsSz(0, 0);
int centroid = 0;
int prv = 0;
while (true) {
bool found = false;
for (int v : adj[centroid])
if (v != prv and 2 * sz[v] >= nbSommet) {
prv = centroid;
centroid = v;
found = true;
break;
}
if (!found)
break;
}
dfs(centroid, centroid);
vector<vector<int>> withSz(nbSommet);
for (int u = 0; u < nbSommet; ++u)
if (u != centroid)
withSz[sz[u]].push_back(u);
vector<int> sol(nbSommet / 2 + 1);
int s = centroid, t = centroid;
int diameter = 0;
for (int d = nbSommet / 2; d >= 1; --d) {
for (int u : withSz[d]) {
int ds = dis(s, u), dt = dis(t, u);
if (ds > diameter)
t = u, diameter = ds;
else if (dt > diameter)
s = u, diameter = dt;
}
sol[d] = diameter + 1;
}
for (int d = 1; d <= nbSommet; ++d) {
if (d % 2)
cout << 1 << '\n';
else
cout << sol[d / 2] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5176 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5104 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
2 ms |
5180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5176 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5104 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
2 ms |
5180 KB |
Output is correct |
22 |
Correct |
7 ms |
6216 KB |
Output is correct |
23 |
Correct |
6 ms |
6100 KB |
Output is correct |
24 |
Correct |
6 ms |
6188 KB |
Output is correct |
25 |
Correct |
5 ms |
6124 KB |
Output is correct |
26 |
Correct |
6 ms |
6212 KB |
Output is correct |
27 |
Correct |
7 ms |
6228 KB |
Output is correct |
28 |
Correct |
6 ms |
6104 KB |
Output is correct |
29 |
Correct |
6 ms |
6188 KB |
Output is correct |
30 |
Correct |
7 ms |
6228 KB |
Output is correct |
31 |
Correct |
6 ms |
6100 KB |
Output is correct |
32 |
Correct |
6 ms |
6316 KB |
Output is correct |
33 |
Correct |
6 ms |
6356 KB |
Output is correct |
34 |
Correct |
5 ms |
6100 KB |
Output is correct |
35 |
Correct |
5 ms |
6192 KB |
Output is correct |
36 |
Correct |
7 ms |
6184 KB |
Output is correct |
37 |
Correct |
6 ms |
6188 KB |
Output is correct |
38 |
Correct |
5 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5176 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5104 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
2 ms |
5180 KB |
Output is correct |
22 |
Correct |
7 ms |
6216 KB |
Output is correct |
23 |
Correct |
6 ms |
6100 KB |
Output is correct |
24 |
Correct |
6 ms |
6188 KB |
Output is correct |
25 |
Correct |
5 ms |
6124 KB |
Output is correct |
26 |
Correct |
6 ms |
6212 KB |
Output is correct |
27 |
Correct |
7 ms |
6228 KB |
Output is correct |
28 |
Correct |
6 ms |
6104 KB |
Output is correct |
29 |
Correct |
6 ms |
6188 KB |
Output is correct |
30 |
Correct |
7 ms |
6228 KB |
Output is correct |
31 |
Correct |
6 ms |
6100 KB |
Output is correct |
32 |
Correct |
6 ms |
6316 KB |
Output is correct |
33 |
Correct |
6 ms |
6356 KB |
Output is correct |
34 |
Correct |
5 ms |
6100 KB |
Output is correct |
35 |
Correct |
5 ms |
6192 KB |
Output is correct |
36 |
Correct |
7 ms |
6184 KB |
Output is correct |
37 |
Correct |
6 ms |
6188 KB |
Output is correct |
38 |
Correct |
5 ms |
6228 KB |
Output is correct |
39 |
Correct |
362 ms |
56428 KB |
Output is correct |
40 |
Correct |
347 ms |
55116 KB |
Output is correct |
41 |
Correct |
358 ms |
56604 KB |
Output is correct |
42 |
Correct |
369 ms |
57336 KB |
Output is correct |
43 |
Correct |
375 ms |
57280 KB |
Output is correct |
44 |
Correct |
352 ms |
57380 KB |
Output is correct |
45 |
Correct |
719 ms |
62316 KB |
Output is correct |
46 |
Correct |
391 ms |
64624 KB |
Output is correct |
47 |
Correct |
244 ms |
57492 KB |
Output is correct |
48 |
Correct |
171 ms |
57416 KB |
Output is correct |
49 |
Correct |
483 ms |
57664 KB |
Output is correct |
50 |
Correct |
191 ms |
57220 KB |
Output is correct |
51 |
Correct |
263 ms |
63800 KB |
Output is correct |