//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b ...);};
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> adj[maxn];
int siz[maxn], dep[maxn], anc[18][maxn];
void dfs(int n, int par, int d) {
anc[0][n] = par;
dep[n] = d;
siz[n] = 1;
for (int v:adj[n]) {
if (v != par) {
dfs(v, n, d + 1);
siz[n] += siz[v];
}
}
}
int tot;
int get_centroid(int n, int par){
for (int v:adj[n]) {
if (v != par && 2 * siz[v] >= tot) {
return get_centroid(v, n);
}
}
return n;
}
int dist(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
int oa = a, ob = b;
int hdif = dep[a] - dep[b], cnt = 0;
while (hdif) {
if (hdif & 1) a = anc[cnt][a];
hdif >>= 1, cnt++;
}
if (a == b) return dep[oa] - dep[ob] + 1;
for (int i = 17;i >= 0;i--) {
if (anc[i][a] != anc[i][b]) {
a = anc[i][a], b = anc[i][b];
}
}
return dep[oa] + dep[ob] - 2 * dep[anc[0][a]] + 1;
}
vector<pii> a;
int ans[maxn];
int main() {
io
int n;
cin >> n;
tot = n;
for (int i = 0;i < n - 1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0, 0);
int root = get_centroid(1, 0);
//debug(root);
dfs(root, 0, 0);
for (int i = 1;i <= n;i++) {
a.push_back({siz[i], i});
}
sort(a.begin(), a.end(), [&](pii x, pii y){return x.ff > y.ff;});
for (int i = 1;i < 18;i++) {
for (int j = 1;j <= n;j++) anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
int p1 = root, p2 = root, ind = 0;
for (int i = n / 2;i > 0;i--) {
while (ind < n && a[ind].ff >= i) {
int p = a[ind].ss;
//debug(p, dist(p, p1), dist(p1, p2));
if (dist(p, p1) > dist(p2, p1)) p2 = p;
if (dist(p, p2) > dist(p1, p2)) p1 = p;
ind++;
}
//debug(p1, p2);
ans[i * 2] = dist(p1, p2);
}
for (int i = 1;i <= n;i++) {
if (i % 2) cout << 1 << "\n";
else cout << ans[i] << "\n";
}
}
/*
5
1 2
2 3
4 2
3 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5032 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5032 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5124 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5024 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
4 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5032 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5032 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5124 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5024 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
4 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
7 ms |
5580 KB |
Output is correct |
23 |
Correct |
6 ms |
5680 KB |
Output is correct |
24 |
Correct |
11 ms |
5676 KB |
Output is correct |
25 |
Correct |
8 ms |
5580 KB |
Output is correct |
26 |
Correct |
7 ms |
5580 KB |
Output is correct |
27 |
Correct |
6 ms |
5580 KB |
Output is correct |
28 |
Correct |
6 ms |
5680 KB |
Output is correct |
29 |
Correct |
7 ms |
5580 KB |
Output is correct |
30 |
Correct |
7 ms |
5580 KB |
Output is correct |
31 |
Correct |
6 ms |
5580 KB |
Output is correct |
32 |
Correct |
7 ms |
5708 KB |
Output is correct |
33 |
Correct |
9 ms |
5836 KB |
Output is correct |
34 |
Correct |
6 ms |
5708 KB |
Output is correct |
35 |
Correct |
6 ms |
5680 KB |
Output is correct |
36 |
Correct |
7 ms |
5708 KB |
Output is correct |
37 |
Correct |
6 ms |
5708 KB |
Output is correct |
38 |
Correct |
6 ms |
5672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5032 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5032 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5124 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5024 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
4 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
7 ms |
5580 KB |
Output is correct |
23 |
Correct |
6 ms |
5680 KB |
Output is correct |
24 |
Correct |
11 ms |
5676 KB |
Output is correct |
25 |
Correct |
8 ms |
5580 KB |
Output is correct |
26 |
Correct |
7 ms |
5580 KB |
Output is correct |
27 |
Correct |
6 ms |
5580 KB |
Output is correct |
28 |
Correct |
6 ms |
5680 KB |
Output is correct |
29 |
Correct |
7 ms |
5580 KB |
Output is correct |
30 |
Correct |
7 ms |
5580 KB |
Output is correct |
31 |
Correct |
6 ms |
5580 KB |
Output is correct |
32 |
Correct |
7 ms |
5708 KB |
Output is correct |
33 |
Correct |
9 ms |
5836 KB |
Output is correct |
34 |
Correct |
6 ms |
5708 KB |
Output is correct |
35 |
Correct |
6 ms |
5680 KB |
Output is correct |
36 |
Correct |
7 ms |
5708 KB |
Output is correct |
37 |
Correct |
6 ms |
5708 KB |
Output is correct |
38 |
Correct |
6 ms |
5672 KB |
Output is correct |
39 |
Correct |
284 ms |
32096 KB |
Output is correct |
40 |
Correct |
283 ms |
31540 KB |
Output is correct |
41 |
Correct |
289 ms |
32200 KB |
Output is correct |
42 |
Correct |
316 ms |
32540 KB |
Output is correct |
43 |
Correct |
328 ms |
32516 KB |
Output is correct |
44 |
Correct |
356 ms |
32464 KB |
Output is correct |
45 |
Correct |
501 ms |
37400 KB |
Output is correct |
46 |
Correct |
284 ms |
41644 KB |
Output is correct |
47 |
Correct |
227 ms |
32976 KB |
Output is correct |
48 |
Correct |
165 ms |
32944 KB |
Output is correct |
49 |
Correct |
355 ms |
33164 KB |
Output is correct |
50 |
Correct |
220 ms |
33132 KB |
Output is correct |
51 |
Correct |
251 ms |
40412 KB |
Output is correct |