#include <bits/stdc++.h>
#pragma GCC optimize("fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse3,sse4,avx,avx2")
#pragma GCC push_options
#pragma GCC pop_options
using namespace std;
const int nmax = 2e5 + 5;
vector<int> g[nmax];
int n;
int rez[nmax];
int supmem[nmax + 100], ptr;
namespace Centroid {
int occ[nmax];
int area[nmax];
int nval[nmax];
int depth[nmax];
#define norm lasama
map<pair<int, int> ,int> norm;
map<int ,int> fpoz;
namespace AINT {
int aint[1048576];
static int len;
void clean(int node = 1, int cl = 1, int cr = len) {
aint[node] = 0;
if(cl == cr)
return;
int mid = cl + cr >> 1;
clean(2 * node, cl, mid);
clean(2 * node + 1, mid + 1, cr);
}
void init(int nlen) {
len = nlen;
clean();
}
void upd(int poz, int val, int node = 1, int cl = 1, int cr = len) {
if(cl == cr)
aint[node] = val;
else {
int mid = cl + cr >> 1;
if(poz <= mid)
upd(poz, val, 2 * node, cl, mid);
else
upd(poz, val, 2 * node + 1, mid + 1, cr);
aint[node] = max(aint[2 * node], aint[2 * node + 1]);
}
return;
}
int query(int poz, int node = 1, int cl = 1, int cr = len) {
if(cr < poz)
return 0;
if(poz <= cl)
return aint[node];
int mid = cl + cr >> 1;
return max(query(poz, 2 * node, cl, mid), query(poz, 2 * node + 1, mid + 1, cr));
}
};
void initcentr(int node, int f) {
area[node] = 1;
for(auto x : g[node]) {
if(occ[x] == 0 && x != f)
initcentr(x, node), area[node] += area[x];
}
return;
}
int initdepth(int node, int f) {
if(occ[node])
return area[node];
depth[node] = depth[f] + 1;
area[node] = 1;
for(auto x : g[node])
if(x != f)
area[node] += initdepth(x, node);
return area[node];
}
int findcentr(int node, int f, int thresh) {
for(auto x : g[node]) {
if(occ[x] || x == f)
continue;
if(area[x] > thresh)
return findcentr(x, node, thresh);
}
return node;
}
void aggr(int node, int f, int val) {
if(val == -1)
AINT::upd(nval[node], 0);
else
AINT::upd(nval[node], depth[node]);
for(auto x : g[node])
if(!occ[x] && x != f)
aggr(x, node, val);
}
void bnorm(int node, int f) {
norm[{area[node], node}];
for(auto x : g[node]) if(!occ[x] && x != f) bnorm(x, node);
return;
}
void query(int node, int f) {
int x = AINT::query(fpoz[area[node]]);
if(x > 0)
rez[(area[node]) * 2] = max(rez[(area[node]) * 2], x + depth[node] - 1);
for(auto x : g[node]) if(!occ[x] && x != f) query(x, node);
return;
}
void mkcentr(int node) {
//cerr << "++++++\n" << node << "\n=====\n";
int temp;
initcentr(node, node);
node = findcentr(node, node, area[node] / 2);
//cerr << "Centroid is: " << node << '\n';
depth[node] = 0;
initdepth(node, node);
for(int x, i = 0; i < g[node].size(); i++) {
if(occ[g[node][i]]) {
swap(g[node][i], g[node].back());
g[node].pop_back();
i--;
continue;
}
x = g[node][i];
norm[{n - area[x], node}];
bnorm(x, node);
}
temp = 0;
for(auto &[a, b] : norm) {
b = ++temp;
if(fpoz.find(a.first) == fpoz.end())
fpoz[a.first] = b;
nval[a.second] = b;
}
if(temp != 0) {
AINT::init(temp);
for(int i = 0; i < g[node].size(); i++)
aggr(g[node][i], node, 1);
for(int i = 0, x; i < g[node].size(); i++) {
x = g[node][i];
aggr(x, node, -1);
AINT::upd(temp = norm[{n - area[x], node}], depth[node]);
query(x, node);
AINT::upd(temp, 0);
aggr(x, node, 1);
}
}
fpoz.clear();
norm.clear();
occ[node] = 1;
int L = ptr, R;
for(auto x : g[node])
supmem[ptr++] = area[x];
R = ptr;
for(auto x : g[node]) {
area[node] = n - supmem[L++];
mkcentr(x);
}
}
}
int what[nmax];
mt19937 rng(time(0));
int rnd(int x) {
return rng() % x;
}
int main() {
srand(time(0));
cin >> n;
//for(int i = 1; i <= n; i++)
//what[i] = i;
//random_shuffle(what + 1, what + n + 1, rnd);
for(int x, y, i = 1; i < n; i++) {
cin >> x >> y;
//x = what[x];
//y = what[y];
g[x].push_back(y);
g[y].push_back(x);
}
Centroid::mkcentr(1);
for(int i = n; i > 0; i--) {
if(i % 2 == 0)
rez[i] = max({1, rez[i + 2], rez[i]});
else
rez[i] = 1;
}
//cout << "1\n" << 2 + rez[2] << '\n';
for(int i = 1; i <= n; i++)
cout << rez[i] << '\n';
}
Compilation message
meetings2.cpp: In function 'void Centroid::AINT::clean(int, int, int)':
meetings2.cpp:29:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid = cl + cr >> 1;
| ~~~^~~~
meetings2.cpp: In function 'void Centroid::AINT::upd(int, int, int, int, int)':
meetings2.cpp:41:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = cl + cr >> 1;
| ~~~^~~~
meetings2.cpp: In function 'int Centroid::AINT::query(int, int, int, int)':
meetings2.cpp:55:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid = cl + cr >> 1;
| ~~~^~~~
meetings2.cpp: In function 'void Centroid::mkcentr(int)':
meetings2.cpp:115:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(int x, i = 0; i < g[node].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
meetings2.cpp:127:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
127 | for(auto &[a, b] : norm) {
| ^
meetings2.cpp:135:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for(int i = 0; i < g[node].size(); i++)
| ~~^~~~~~~~~~~~~~~~
meetings2.cpp:137:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(int i = 0, x; i < g[node].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
meetings2.cpp:149:18: warning: variable 'R' set but not used [-Wunused-but-set-variable]
149 | int L = ptr, R;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4984 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4984 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
18 ms |
5460 KB |
Output is correct |
23 |
Correct |
18 ms |
5580 KB |
Output is correct |
24 |
Correct |
19 ms |
5572 KB |
Output is correct |
25 |
Correct |
19 ms |
5568 KB |
Output is correct |
26 |
Correct |
21 ms |
5544 KB |
Output is correct |
27 |
Correct |
19 ms |
5560 KB |
Output is correct |
28 |
Correct |
17 ms |
5560 KB |
Output is correct |
29 |
Correct |
18 ms |
5500 KB |
Output is correct |
30 |
Correct |
18 ms |
5576 KB |
Output is correct |
31 |
Correct |
20 ms |
5588 KB |
Output is correct |
32 |
Correct |
26 ms |
5744 KB |
Output is correct |
33 |
Correct |
23 ms |
5840 KB |
Output is correct |
34 |
Correct |
9 ms |
5536 KB |
Output is correct |
35 |
Correct |
8 ms |
5496 KB |
Output is correct |
36 |
Correct |
18 ms |
5544 KB |
Output is correct |
37 |
Correct |
11 ms |
5584 KB |
Output is correct |
38 |
Correct |
16 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4984 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5016 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
18 ms |
5460 KB |
Output is correct |
23 |
Correct |
18 ms |
5580 KB |
Output is correct |
24 |
Correct |
19 ms |
5572 KB |
Output is correct |
25 |
Correct |
19 ms |
5568 KB |
Output is correct |
26 |
Correct |
21 ms |
5544 KB |
Output is correct |
27 |
Correct |
19 ms |
5560 KB |
Output is correct |
28 |
Correct |
17 ms |
5560 KB |
Output is correct |
29 |
Correct |
18 ms |
5500 KB |
Output is correct |
30 |
Correct |
18 ms |
5576 KB |
Output is correct |
31 |
Correct |
20 ms |
5588 KB |
Output is correct |
32 |
Correct |
26 ms |
5744 KB |
Output is correct |
33 |
Correct |
23 ms |
5840 KB |
Output is correct |
34 |
Correct |
9 ms |
5536 KB |
Output is correct |
35 |
Correct |
8 ms |
5496 KB |
Output is correct |
36 |
Correct |
18 ms |
5544 KB |
Output is correct |
37 |
Correct |
11 ms |
5584 KB |
Output is correct |
38 |
Correct |
16 ms |
5716 KB |
Output is correct |
39 |
Correct |
2253 ms |
30904 KB |
Output is correct |
40 |
Correct |
2113 ms |
30464 KB |
Output is correct |
41 |
Correct |
2167 ms |
31036 KB |
Output is correct |
42 |
Correct |
2071 ms |
31528 KB |
Output is correct |
43 |
Correct |
2054 ms |
31420 KB |
Output is correct |
44 |
Correct |
2071 ms |
31476 KB |
Output is correct |
45 |
Correct |
3381 ms |
39768 KB |
Output is correct |
46 |
Correct |
2702 ms |
45544 KB |
Output is correct |
47 |
Correct |
607 ms |
31728 KB |
Output is correct |
48 |
Correct |
516 ms |
32000 KB |
Output is correct |
49 |
Correct |
2280 ms |
31976 KB |
Output is correct |
50 |
Correct |
874 ms |
32152 KB |
Output is correct |
51 |
Correct |
1669 ms |
38732 KB |
Output is correct |