#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
vector<pair<int, int> > link[200002];
int ex[400002], ey[400002];
int DP[400002];
void dfs(int x, int p=-1, int en = -1){
if(en >= 0) DP[en] = 1;
int opp = -1;
for(auto y: link[x]){
if(y.first == p) opp = y.second;
else{
dfs(y.first, x, y.second);
DP[en] += DP[y.second];
}
}
if(en >= 0) DP[opp] = n-DP[en];
}
bool centroidUsed[200002];
int subTreeSize[200002];
int ans[200002];
void cdfs1(int x, int p=-1){
subTreeSize[x] = 1;
for(auto y: link[x]){
if(y.first == p || centroidUsed[y.first]) continue;
cdfs1(y.first, x);
subTreeSize[x] += subTreeSize[y.first];
}
}
int cdfs2(int x, int p, int lim){
for(auto y: link[x]){
if(y.first == p || centroidUsed[y.first]) continue;
if(subTreeSize[y.first] >= lim) return cdfs2(y.first, x, lim);
}
return x;
}
struct dat{
int dist, group, sz;
dat(){}
dat(int dist, int group, int sz): dist(dist), group(group), sz(sz){}
bool operator<(const dat &r)const{
return sz>r.sz;
}
};
dat group[2];
int track(int x, int p, int g, int d, int opp, vector<dat> &vec){
int sum = 1;
for(auto y: link[x]){
if(y.first == p || centroidUsed[y.first]) continue;
sum += track(y.first, x, g, d+1, opp, vec);
}
vec.push_back(dat(d, g, sum));
ans[min(opp, sum)] = max(ans[min(opp, sum)], d);
return sum;
}
void findCentroid(int x){
cdfs1(x);
x = cdfs2(x, -1, (subTreeSize[x]+1)/2);
centroidUsed[x] = 1;
vector<dat> vec;
for(auto y: link[x]){
if(centroidUsed[y.first]) continue;
track(y.first, x, y.first, 1, DP[y.second^1], vec);
}
sort(vec.begin(), vec.end());
group[0] = group[1] = dat(-1e9, -1, -1e9);
for(dat p: vec){
if(p.group == group[0].group) group[0].dist = max(group[0].dist, p.dist);
else if(p.group == group[1].group){
group[1].dist = max(group[1].dist, p.dist);
if(group[1].dist > group[0].dist) swap(group[0], group[1]);
}
else{
if(group[1].dist < p.dist) group[1] = p;
if(group[1].dist > group[0].dist) swap(group[0], group[1]);
}
ans[p.sz] = max(ans[p.sz], group[0].dist + group[1].dist);
}
for(auto y: link[x]){
if(centroidUsed[y.first]) continue;
findCentroid(y.first);
}
}
int main(){
scanf("%d", &n);
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(make_pair(y, i*2-2));
link[y].push_back(make_pair(x, i*2-1));
ex[i*2-2] = ey[i*2-1] = x;
ex[i*2-1] = ey[i*2-2] = y;
}
dfs(1);
findCentroid(1);
for(int i=n; i>=1; i--) ans[i] = max(ans[i+1], ans[i]);
for(int i=1; i<=n; i++){
if(i&1) printf("1\n");
else printf("%d\n", ans[i/2]+1);
}
}
Compilation message
meetings2.cpp: In function 'int main()':
meetings2.cpp:101:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
meetings2.cpp:104:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
4960 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
4960 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
5 ms |
5332 KB |
Output is correct |
23 |
Correct |
7 ms |
5332 KB |
Output is correct |
24 |
Correct |
6 ms |
5404 KB |
Output is correct |
25 |
Correct |
6 ms |
5320 KB |
Output is correct |
26 |
Correct |
7 ms |
5332 KB |
Output is correct |
27 |
Correct |
6 ms |
5332 KB |
Output is correct |
28 |
Correct |
6 ms |
5332 KB |
Output is correct |
29 |
Correct |
6 ms |
5332 KB |
Output is correct |
30 |
Correct |
6 ms |
5340 KB |
Output is correct |
31 |
Correct |
6 ms |
5332 KB |
Output is correct |
32 |
Correct |
7 ms |
5460 KB |
Output is correct |
33 |
Correct |
7 ms |
5584 KB |
Output is correct |
34 |
Correct |
5 ms |
5332 KB |
Output is correct |
35 |
Correct |
4 ms |
5332 KB |
Output is correct |
36 |
Correct |
5 ms |
5332 KB |
Output is correct |
37 |
Correct |
4 ms |
5332 KB |
Output is correct |
38 |
Correct |
6 ms |
5492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
4960 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
5 ms |
5332 KB |
Output is correct |
23 |
Correct |
7 ms |
5332 KB |
Output is correct |
24 |
Correct |
6 ms |
5404 KB |
Output is correct |
25 |
Correct |
6 ms |
5320 KB |
Output is correct |
26 |
Correct |
7 ms |
5332 KB |
Output is correct |
27 |
Correct |
6 ms |
5332 KB |
Output is correct |
28 |
Correct |
6 ms |
5332 KB |
Output is correct |
29 |
Correct |
6 ms |
5332 KB |
Output is correct |
30 |
Correct |
6 ms |
5340 KB |
Output is correct |
31 |
Correct |
6 ms |
5332 KB |
Output is correct |
32 |
Correct |
7 ms |
5460 KB |
Output is correct |
33 |
Correct |
7 ms |
5584 KB |
Output is correct |
34 |
Correct |
5 ms |
5332 KB |
Output is correct |
35 |
Correct |
4 ms |
5332 KB |
Output is correct |
36 |
Correct |
5 ms |
5332 KB |
Output is correct |
37 |
Correct |
4 ms |
5332 KB |
Output is correct |
38 |
Correct |
6 ms |
5492 KB |
Output is correct |
39 |
Correct |
394 ms |
22940 KB |
Output is correct |
40 |
Correct |
351 ms |
22516 KB |
Output is correct |
41 |
Correct |
417 ms |
23128 KB |
Output is correct |
42 |
Correct |
379 ms |
23040 KB |
Output is correct |
43 |
Correct |
387 ms |
23112 KB |
Output is correct |
44 |
Correct |
378 ms |
23020 KB |
Output is correct |
45 |
Correct |
600 ms |
28776 KB |
Output is correct |
46 |
Correct |
542 ms |
33348 KB |
Output is correct |
47 |
Correct |
185 ms |
21760 KB |
Output is correct |
48 |
Correct |
114 ms |
22820 KB |
Output is correct |
49 |
Correct |
302 ms |
23576 KB |
Output is correct |
50 |
Correct |
134 ms |
23564 KB |
Output is correct |
51 |
Correct |
318 ms |
30564 KB |
Output is correct |