#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<int,int>
#define Pii pair<pair<int,int> , pair<int,int> >
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t, sz[N], f[N], h;
int n, c;
int ans[N];
map<int,int> SZ[N];
vector<pair<int, pair<int,int> > > x;
vector<int> V[N];
int find(int u,int p, int Sz) {
for(int i = 0; i< V[u].size(); i++) {
if(V[u][i] == p|| f[V[u][i]]) continue;
if(sz[V[u][i]] > Sz / 2) return find(V[u][i], u, Sz);
}
return u;
}
void dfs(int u, int p) {
sz[u] = 1;
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p || f[V[u][i]]) continue;
dfs(V[u][i], u);
sz[u] += sz[V[u][i]];
}
}
void dfs(int u,int p,int P) {
++h;
x.push_back({SZ[p][u], {h, P}});
ans[min(SZ[p][u], n - SZ[c][P]) * 2] = max(ans[min(SZ[p][u], n - SZ[c][P]) * 2], h + 1);
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p || f[V[u][i]]) continue;
dfs(V[u][i], u, P);
}
--h;
}
void upd(Pii &mx, pii x) {
if(x.s == mx.f.s) mx.f = max(mx.f, x);
else mx.s = max(mx.s, x);
if(mx.f.f < mx.s.f) swap(mx.s, mx.f);
}
void decomp(int U) {
dfs(U, 0);
c = find(U, 0, sz[U]);
for(int i = 0; i < V[c].size(); i++) {
if(f[V[c][i]] == 0) dfs(V[c][i], c, V[c][i]);
}
pair<pii, pii> mx;
mx = {{-n, 0}, {-n, 0}};
sort(x.begin(), x.end());
while(x.size()) {
int i = x.back().f;
pii y = x.back().s;
ans[i * 2] = max(ans[i * 2], y.f + (mx.f.s != y.s ? mx.f.f : mx.s.f ) + 1);
upd(mx, y);
x.pop_back();
}
f[c] = 1;
int C = c;
for(int i = 0; i < V[C].size(); i++) {
if(!f[V[C][i]]) decomp(V[C][i]);
}
}
void dfss(int u,int p) {
sz[u] = 1;
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p) continue;
dfss(V[u][i], u);
SZ[u][V[u][i]] = sz[V[u][i]];
sz[u] += sz[V[u][i]];
}
SZ[u][p] = n - sz[u];
}
main(){
cin >> n;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
V[u].push_back(v);
V[v].push_back(u);
}
dfss(1, 0);
decomp(1);
for(int i = n; i >= 1; i--) {
ans[i] = (i % 2 == 0 ? max(ans[i], ans[i + 2]) : 1);
}
for(int i = 1; i <= n; i++) {
ans[i] = max(ans[i], 1);
cout << ans[i] << "\n";
}
}
Compilation message
meetings2.cpp: In function 'int find(int, int, int)':
meetings2.cpp:15:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i = 0; i< V[u].size(); i++) {
| ~^~~~~~~~~~~~~
meetings2.cpp: In function 'void dfs(int, int)':
meetings2.cpp:23:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
meetings2.cpp: In function 'void dfs(int, int, int)':
meetings2.cpp:33:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
meetings2.cpp: In function 'void decomp(int)':
meetings2.cpp:47:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i = 0; i < V[c].size(); i++) {
| ~~^~~~~~~~~~~~~
meetings2.cpp:63:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < V[C].size(); i++) {
| ~~^~~~~~~~~~~~~
meetings2.cpp: In function 'void dfss(int, int)':
meetings2.cpp:69:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
meetings2.cpp: At global scope:
meetings2.cpp:77:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
77 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
7 ms |
14292 KB |
Output is correct |
7 |
Correct |
7 ms |
14292 KB |
Output is correct |
8 |
Correct |
9 ms |
14292 KB |
Output is correct |
9 |
Correct |
9 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14292 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14292 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
8 ms |
14292 KB |
Output is correct |
15 |
Correct |
7 ms |
14292 KB |
Output is correct |
16 |
Correct |
9 ms |
14376 KB |
Output is correct |
17 |
Correct |
7 ms |
14292 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
7 ms |
14484 KB |
Output is correct |
20 |
Correct |
7 ms |
14292 KB |
Output is correct |
21 |
Correct |
10 ms |
14296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
7 ms |
14292 KB |
Output is correct |
7 |
Correct |
7 ms |
14292 KB |
Output is correct |
8 |
Correct |
9 ms |
14292 KB |
Output is correct |
9 |
Correct |
9 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14292 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14292 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
8 ms |
14292 KB |
Output is correct |
15 |
Correct |
7 ms |
14292 KB |
Output is correct |
16 |
Correct |
9 ms |
14376 KB |
Output is correct |
17 |
Correct |
7 ms |
14292 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
7 ms |
14484 KB |
Output is correct |
20 |
Correct |
7 ms |
14292 KB |
Output is correct |
21 |
Correct |
10 ms |
14296 KB |
Output is correct |
22 |
Correct |
17 ms |
15012 KB |
Output is correct |
23 |
Correct |
14 ms |
15064 KB |
Output is correct |
24 |
Correct |
15 ms |
15060 KB |
Output is correct |
25 |
Correct |
15 ms |
15032 KB |
Output is correct |
26 |
Correct |
14 ms |
15044 KB |
Output is correct |
27 |
Correct |
14 ms |
15064 KB |
Output is correct |
28 |
Correct |
14 ms |
15060 KB |
Output is correct |
29 |
Correct |
14 ms |
15044 KB |
Output is correct |
30 |
Correct |
14 ms |
15060 KB |
Output is correct |
31 |
Correct |
16 ms |
15000 KB |
Output is correct |
32 |
Correct |
16 ms |
15156 KB |
Output is correct |
33 |
Correct |
14 ms |
15336 KB |
Output is correct |
34 |
Correct |
12 ms |
15048 KB |
Output is correct |
35 |
Correct |
17 ms |
15036 KB |
Output is correct |
36 |
Correct |
15 ms |
15060 KB |
Output is correct |
37 |
Correct |
13 ms |
15004 KB |
Output is correct |
38 |
Correct |
14 ms |
15188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
7 ms |
14292 KB |
Output is correct |
7 |
Correct |
7 ms |
14292 KB |
Output is correct |
8 |
Correct |
9 ms |
14292 KB |
Output is correct |
9 |
Correct |
9 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14292 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14292 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
8 ms |
14292 KB |
Output is correct |
15 |
Correct |
7 ms |
14292 KB |
Output is correct |
16 |
Correct |
9 ms |
14376 KB |
Output is correct |
17 |
Correct |
7 ms |
14292 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
7 ms |
14484 KB |
Output is correct |
20 |
Correct |
7 ms |
14292 KB |
Output is correct |
21 |
Correct |
10 ms |
14296 KB |
Output is correct |
22 |
Correct |
17 ms |
15012 KB |
Output is correct |
23 |
Correct |
14 ms |
15064 KB |
Output is correct |
24 |
Correct |
15 ms |
15060 KB |
Output is correct |
25 |
Correct |
15 ms |
15032 KB |
Output is correct |
26 |
Correct |
14 ms |
15044 KB |
Output is correct |
27 |
Correct |
14 ms |
15064 KB |
Output is correct |
28 |
Correct |
14 ms |
15060 KB |
Output is correct |
29 |
Correct |
14 ms |
15044 KB |
Output is correct |
30 |
Correct |
14 ms |
15060 KB |
Output is correct |
31 |
Correct |
16 ms |
15000 KB |
Output is correct |
32 |
Correct |
16 ms |
15156 KB |
Output is correct |
33 |
Correct |
14 ms |
15336 KB |
Output is correct |
34 |
Correct |
12 ms |
15048 KB |
Output is correct |
35 |
Correct |
17 ms |
15036 KB |
Output is correct |
36 |
Correct |
15 ms |
15060 KB |
Output is correct |
37 |
Correct |
13 ms |
15004 KB |
Output is correct |
38 |
Correct |
14 ms |
15188 KB |
Output is correct |
39 |
Correct |
730 ms |
44468 KB |
Output is correct |
40 |
Correct |
730 ms |
46040 KB |
Output is correct |
41 |
Correct |
732 ms |
46772 KB |
Output is correct |
42 |
Correct |
723 ms |
47392 KB |
Output is correct |
43 |
Correct |
766 ms |
47368 KB |
Output is correct |
44 |
Correct |
730 ms |
47488 KB |
Output is correct |
45 |
Correct |
1072 ms |
54204 KB |
Output is correct |
46 |
Correct |
835 ms |
59800 KB |
Output is correct |
47 |
Correct |
537 ms |
47992 KB |
Output is correct |
48 |
Correct |
556 ms |
48504 KB |
Output is correct |
49 |
Correct |
899 ms |
48028 KB |
Output is correct |
50 |
Correct |
829 ms |
48280 KB |
Output is correct |
51 |
Correct |
815 ms |
58516 KB |
Output is correct |