#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 2e5 + 5;
int n;
int timer, in[N], out[N], dep[N], tot[N], ans[N], P[N][20];
vector < int > v[N];
set < int > st[N];
bool is_anc(int a, int b) {
return (in[a] <= in[b] && out[b] <= out[a]);
}
int LCA(int a, int b) {
if (dep[a] > dep[b]) swap(a, b);
if (is_anc(a, b)) return a;
for (int j = 18; j >= 0; --j)
if (!is_anc(P[a][j], b)) a = P[a][j];
return P[a][0];
}
int dst(int a, int b) {
return dep[a] + dep[b] - 2 * dep[LCA(a, b)] + 1;
}
void dfs(int x, int p) {
dep[x] = dep[p] + 1;
in[x] = ++timer;
P[x][0] = p;
for (int j = 1; j <= 18; ++j) {
P[x][j] = P[P[x][j - 1]][j - 1];
}
for (int i = 0; i < v[x].size(); ++i) {
int to = v[x][i];
if (to == p) continue;
dfs(to, x);
}
out[x] = timer;
}
main () {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n;
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
st[a].insert(b);
st[b].insert(a);
}
dfs(1, 1);
set < pair < int , int > > S;
for (int i = 1; i <= n; ++i) {
tot[i] = 1;
if (v[i].size() == 1) {
S.insert({tot[i], i});
}
}
vector < pair < int , int > > rec;
while (S.size()) {
int x = (*S.begin()).s;
S.erase(S.find(*S.begin()));
if (st[x].size() == 1) {
int y = (*st[x].begin());
rec.pb({x, tot[x]});
tot[y] += tot[x];
st[y].erase(st[y].find(x));
st[x].erase(st[x].find(y));
if (st[y].size() == 1) {
S.insert({tot[y], y});
}
}
}
int str = 1;
for (int i = 1; i <= n; ++i) {
if (tot[i] == n) str = i;
}
int a0 = str, b0 = str, dm = 1;
for (int i = n; i >= 1; --i) {
if (i % 2 == 1) {
ans[i] = 1;
}
else {
while (rec.size() && rec.back().s >= i / 2) {
int x = rec.back().f, a1 = dst(a0, x), b1 = dst(b0, x);
rec.pop_back();
if (a1 <= b1) {
if (dm < b1) dm = b1, a0 = x;
}
else
if (b1 < a1) {
if (dm < a1) dm = a1, b0 = x;
}
}
ans[i] = dm;
}
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << "\n";
}
}
Compilation message
meetings2.cpp: In function 'void dfs(int, int)':
meetings2.cpp:39:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 0; i < v[x].size(); ++i) {
| ~~^~~~~~~~~~~~~
meetings2.cpp: At global scope:
meetings2.cpp:47:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
47 | main () {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14396 KB |
Output is correct |
3 |
Correct |
10 ms |
14384 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
11 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14324 KB |
Output is correct |
11 |
Correct |
7 ms |
14344 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14428 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14360 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14408 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14396 KB |
Output is correct |
3 |
Correct |
10 ms |
14384 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
11 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14324 KB |
Output is correct |
11 |
Correct |
7 ms |
14344 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14428 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14360 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14408 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14416 KB |
Output is correct |
22 |
Correct |
14 ms |
15508 KB |
Output is correct |
23 |
Correct |
13 ms |
15464 KB |
Output is correct |
24 |
Correct |
13 ms |
15444 KB |
Output is correct |
25 |
Correct |
14 ms |
15456 KB |
Output is correct |
26 |
Correct |
13 ms |
15444 KB |
Output is correct |
27 |
Correct |
13 ms |
15456 KB |
Output is correct |
28 |
Correct |
16 ms |
15536 KB |
Output is correct |
29 |
Correct |
14 ms |
15444 KB |
Output is correct |
30 |
Correct |
13 ms |
15468 KB |
Output is correct |
31 |
Correct |
12 ms |
15464 KB |
Output is correct |
32 |
Correct |
12 ms |
15572 KB |
Output is correct |
33 |
Correct |
12 ms |
15540 KB |
Output is correct |
34 |
Correct |
13 ms |
15456 KB |
Output is correct |
35 |
Correct |
13 ms |
15584 KB |
Output is correct |
36 |
Correct |
13 ms |
15444 KB |
Output is correct |
37 |
Correct |
14 ms |
15576 KB |
Output is correct |
38 |
Correct |
16 ms |
15560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14396 KB |
Output is correct |
3 |
Correct |
10 ms |
14384 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
11 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14324 KB |
Output is correct |
11 |
Correct |
7 ms |
14344 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14428 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14360 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14408 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14416 KB |
Output is correct |
22 |
Correct |
14 ms |
15508 KB |
Output is correct |
23 |
Correct |
13 ms |
15464 KB |
Output is correct |
24 |
Correct |
13 ms |
15444 KB |
Output is correct |
25 |
Correct |
14 ms |
15456 KB |
Output is correct |
26 |
Correct |
13 ms |
15444 KB |
Output is correct |
27 |
Correct |
13 ms |
15456 KB |
Output is correct |
28 |
Correct |
16 ms |
15536 KB |
Output is correct |
29 |
Correct |
14 ms |
15444 KB |
Output is correct |
30 |
Correct |
13 ms |
15468 KB |
Output is correct |
31 |
Correct |
12 ms |
15464 KB |
Output is correct |
32 |
Correct |
12 ms |
15572 KB |
Output is correct |
33 |
Correct |
12 ms |
15540 KB |
Output is correct |
34 |
Correct |
13 ms |
15456 KB |
Output is correct |
35 |
Correct |
13 ms |
15584 KB |
Output is correct |
36 |
Correct |
13 ms |
15444 KB |
Output is correct |
37 |
Correct |
14 ms |
15576 KB |
Output is correct |
38 |
Correct |
16 ms |
15560 KB |
Output is correct |
39 |
Correct |
594 ms |
67468 KB |
Output is correct |
40 |
Correct |
571 ms |
66432 KB |
Output is correct |
41 |
Correct |
587 ms |
67644 KB |
Output is correct |
42 |
Correct |
592 ms |
68552 KB |
Output is correct |
43 |
Correct |
559 ms |
68580 KB |
Output is correct |
44 |
Correct |
553 ms |
68552 KB |
Output is correct |
45 |
Correct |
530 ms |
71332 KB |
Output is correct |
46 |
Correct |
355 ms |
71144 KB |
Output is correct |
47 |
Correct |
504 ms |
70828 KB |
Output is correct |
48 |
Correct |
493 ms |
74044 KB |
Output is correct |
49 |
Correct |
460 ms |
69024 KB |
Output is correct |
50 |
Correct |
438 ms |
73868 KB |
Output is correct |
51 |
Correct |
394 ms |
74756 KB |
Output is correct |