#include <iostream>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
int s[200005], l[200005][19], d[200005], c[200005], dd[200005], da[200005], z = 0, ff[200005];
vector<int> g[200005];
vector<pair<int, int>> gg[200005];
set<int> t;
void dfs(int x, int p) {
s[x] = 1;
dd[x] = ++z;
l[x][0] = p;
for (int w : g[x]) {
if (w == p) continue;
c[x]++;
d[w] = d[x] + 1;
dfs(w, x);
s[x] += s[w];
}
if (p != 0 && g[x].size() == 1) t.insert(x);
da[x] = z;
}
int lca(int x, int y) {
if (d[x] < d[y]) swap(x, y);
int k = d[x] - d[y];
for (int i = 18; i >= 0; i--) {
if (k & (1 << i)) {
x = l[x][i];
}
}
if (x == y) return x;
for (int i = 18; i >= 0; i--) {
if (l[x][i] != l[y][i]) {
x = l[x][i];
y = l[y][i];
}
}
return l[x][0];
}
bool cmp(int x, int y) {
return dd[x] < dd[y];
}
int ma = 1;
void dfs2(int x, int p) {
ff[x] = 0;
for (auto w : gg[x]) {
if (w.first == p) continue;
//cout << x << " " << w.first << " " << w.second << '\n';
dfs2(w.first, x);
ma = max(ma, ff[w.first] + ff[x] + w.second + 1);
ff[x] = max(ff[x], ff[w.first] + w.second);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i <= 18; i++) {
for (int j = 1; j <= n; j++) {
l[j][i] = l[l[j][i - 1]][i - 1];
}
}
for (int i = 1; i <= n; i++) {
if (i & 1) {
cout << 1 << '\n';
continue;
}
vector<int> v, vb, vv, va;
ma = 1;
for (int w : t) {
if (n - s[w] < i / 2) continue;
int res = 0, k = w;
for (int j = 18; j >= 0; j--) {
if (l[k][j] == 0) continue;
if (n - s[l[k][j]] >= i / 2) {
k = l[k][j];
res += (1 << j);
}
}
ma = max(ma, res + 2);
}
for (int w : t) {
v.push_back(w);
vb.push_back(w);
}
sort(vb.begin(), vb.end(), cmp);
for (int i = 1; i < vb.size(); i++) v.push_back(lca(vb[i], vb[i - 1]));
sort(v.begin(), v.end(), cmp);
int h = 0;
stack<int> ss;
for (int w : v) {
if (w == h) continue;
h = w;
while (!ss.empty()) {
int p = ss.top();
if (dd[p] <= dd[w] && da[w] <= da[p]) {
gg[p].push_back({w, d[w] - d[p]});
break;
}
ss.pop();
}
ss.push(h);
}
while (ss.size() > 1) ss.pop();
dfs2(ss.top(), -1);
for (int w : vb) {
if (s[w] == i / 2) {
vv.push_back(w);
c[l[w][0]]--;
if (c[l[w][0]] == 0) va.push_back(l[w][0]);
}
}
for (int w : v) gg[w].clear();
for (int w : vv) t.erase(w);
for (int w : va) t.insert(w);
cout << ma << '\n';
}
}
Compilation message
meetings2.cpp: In function 'int main()':
meetings2.cpp:97:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 1; i < vb.size(); i++) v.push_back(lca(vb[i], vb[i - 1]));
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
2 ms |
16988 KB |
Output is correct |
5 |
Correct |
2 ms |
16988 KB |
Output is correct |
6 |
Correct |
2 ms |
16988 KB |
Output is correct |
7 |
Correct |
2 ms |
16984 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
2 ms |
16984 KB |
Output is correct |
11 |
Correct |
2 ms |
16984 KB |
Output is correct |
12 |
Correct |
2 ms |
17044 KB |
Output is correct |
13 |
Correct |
2 ms |
16988 KB |
Output is correct |
14 |
Correct |
2 ms |
17048 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
2 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
2 ms |
16832 KB |
Output is correct |
19 |
Correct |
3 ms |
16984 KB |
Output is correct |
20 |
Correct |
2 ms |
16988 KB |
Output is correct |
21 |
Correct |
2 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
2 ms |
16988 KB |
Output is correct |
5 |
Correct |
2 ms |
16988 KB |
Output is correct |
6 |
Correct |
2 ms |
16988 KB |
Output is correct |
7 |
Correct |
2 ms |
16984 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
2 ms |
16984 KB |
Output is correct |
11 |
Correct |
2 ms |
16984 KB |
Output is correct |
12 |
Correct |
2 ms |
17044 KB |
Output is correct |
13 |
Correct |
2 ms |
16988 KB |
Output is correct |
14 |
Correct |
2 ms |
17048 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
2 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
2 ms |
16832 KB |
Output is correct |
19 |
Correct |
3 ms |
16984 KB |
Output is correct |
20 |
Correct |
2 ms |
16988 KB |
Output is correct |
21 |
Correct |
2 ms |
16988 KB |
Output is correct |
22 |
Correct |
8 ms |
17240 KB |
Output is correct |
23 |
Correct |
8 ms |
17244 KB |
Output is correct |
24 |
Correct |
7 ms |
17244 KB |
Output is correct |
25 |
Correct |
8 ms |
17392 KB |
Output is correct |
26 |
Correct |
7 ms |
17244 KB |
Output is correct |
27 |
Correct |
8 ms |
17244 KB |
Output is correct |
28 |
Correct |
7 ms |
17260 KB |
Output is correct |
29 |
Correct |
7 ms |
17244 KB |
Output is correct |
30 |
Correct |
7 ms |
17300 KB |
Output is correct |
31 |
Correct |
8 ms |
17244 KB |
Output is correct |
32 |
Correct |
7 ms |
17244 KB |
Output is correct |
33 |
Correct |
4 ms |
17240 KB |
Output is correct |
34 |
Correct |
6 ms |
17244 KB |
Output is correct |
35 |
Correct |
5 ms |
17500 KB |
Output is correct |
36 |
Correct |
6 ms |
17244 KB |
Output is correct |
37 |
Correct |
6 ms |
17476 KB |
Output is correct |
38 |
Correct |
5 ms |
17460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
2 ms |
16988 KB |
Output is correct |
5 |
Correct |
2 ms |
16988 KB |
Output is correct |
6 |
Correct |
2 ms |
16988 KB |
Output is correct |
7 |
Correct |
2 ms |
16984 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
2 ms |
16984 KB |
Output is correct |
11 |
Correct |
2 ms |
16984 KB |
Output is correct |
12 |
Correct |
2 ms |
17044 KB |
Output is correct |
13 |
Correct |
2 ms |
16988 KB |
Output is correct |
14 |
Correct |
2 ms |
17048 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
2 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
2 ms |
16832 KB |
Output is correct |
19 |
Correct |
3 ms |
16984 KB |
Output is correct |
20 |
Correct |
2 ms |
16988 KB |
Output is correct |
21 |
Correct |
2 ms |
16988 KB |
Output is correct |
22 |
Correct |
8 ms |
17240 KB |
Output is correct |
23 |
Correct |
8 ms |
17244 KB |
Output is correct |
24 |
Correct |
7 ms |
17244 KB |
Output is correct |
25 |
Correct |
8 ms |
17392 KB |
Output is correct |
26 |
Correct |
7 ms |
17244 KB |
Output is correct |
27 |
Correct |
8 ms |
17244 KB |
Output is correct |
28 |
Correct |
7 ms |
17260 KB |
Output is correct |
29 |
Correct |
7 ms |
17244 KB |
Output is correct |
30 |
Correct |
7 ms |
17300 KB |
Output is correct |
31 |
Correct |
8 ms |
17244 KB |
Output is correct |
32 |
Correct |
7 ms |
17244 KB |
Output is correct |
33 |
Correct |
4 ms |
17240 KB |
Output is correct |
34 |
Correct |
6 ms |
17244 KB |
Output is correct |
35 |
Correct |
5 ms |
17500 KB |
Output is correct |
36 |
Correct |
6 ms |
17244 KB |
Output is correct |
37 |
Correct |
6 ms |
17476 KB |
Output is correct |
38 |
Correct |
5 ms |
17460 KB |
Output is correct |
39 |
Correct |
497 ms |
46992 KB |
Output is correct |
40 |
Correct |
482 ms |
46532 KB |
Output is correct |
41 |
Correct |
473 ms |
47216 KB |
Output is correct |
42 |
Correct |
485 ms |
47216 KB |
Output is correct |
43 |
Correct |
525 ms |
47216 KB |
Output is correct |
44 |
Correct |
530 ms |
47596 KB |
Output is correct |
45 |
Correct |
288 ms |
50208 KB |
Output is correct |
46 |
Correct |
184 ms |
47696 KB |
Output is correct |
47 |
Correct |
282 ms |
50112 KB |
Output is correct |
48 |
Correct |
294 ms |
53832 KB |
Output is correct |
49 |
Correct |
302 ms |
46616 KB |
Output is correct |
50 |
Correct |
293 ms |
54380 KB |
Output is correct |
51 |
Correct |
216 ms |
53572 KB |
Output is correct |