#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define int long long
int ckmin(int &a, int b){return (b<a)?a=b:a;}
int ckmax(int &a, int b){return (b>a)?a=b:b;}
int n;
vector<int> adj[MAXN];
int siz[MAXN];
bool vis[MAXN];
int ans[MAXN];
int tmpsiz[MAXN];
int depth[MAXN];
set<array<int, 3>, greater<>> st;
void dfs(int v, int p){
siz[v] = 1;
for(auto u : adj[v]){
if(u == p || vis[u]) continue;
dfs(u, v);
siz[v] += siz[u];
}
}
int centroid(int v){
dfs(v, -1);
int cursiz = siz[v], p = -1;
do{
int nxt = -1;
for(auto u : adj[v]){
if(u == p || vis[u]) continue;
if(2*siz[u] > cursiz) nxt = u;
}
p = v, v = nxt;
} while(~v);
return p;
}
void dfs2(int v, int p, int d){
depth[v] = d;
tmpsiz[v] = 1;
for(auto u : adj[v]){
if(u == p || vis[u]) continue;
dfs2(u, v, d+1);
tmpsiz[v] += tmpsiz[u];
}
}
void calc(int v, int p, int r, int c){
st.insert({tmpsiz[v], depth[v], r});
int k = 2*min(tmpsiz[v], tmpsiz[c]-tmpsiz[r]);
ckmax(ans[k], depth[v]+1);
for(auto u : adj[v]){
if(u == p || vis[u]) continue;
calc(u, v, r, c);
}
}
void solve(int c){
st.clear();
dfs2(c, -1, 0);
for(auto u : adj[c]){
if(vis[u]) continue;
calc(u, c, u, c);
}
multiset<int, greater<>> mst;
map<int, int> mp;
for(auto x : st){
int s = x[0], d = x[1], r = x[2];
if(mp[r] < d){
if(mp[r] > 0) mst.erase(mst.find(mp[r]));
mp[r] = d;
mst.insert(d);
}
if((int)mst.size() >= 2){
int len = (*mst.begin())+(*next(mst.begin()))+1;
ckmax(ans[2*s], len);
}
}
}
void decomp(int v, int p){
int c = centroid(v);
solve(c);
vis[c] = 1;
for(auto u : adj[c]){
if(vis[u]) continue;
decomp(u, c);
}
}
signed main(){
// freopen("a.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 0; i<n-1; i++){
int a, b; cin >> a >> b; --a; --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i = 1; i<=n; i++) ans[i] = 1;
decomp(0, -1);
for(int i = n; i>=0; i--) ckmax(ans[i], ans[i+1]);
for(int i = 1; i<=n; i++){
if(i%2) cout << 1 << endl;
else cout << ans[i] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
5020 KB |
Output is correct |
9 |
Correct |
3 ms |
5020 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5024 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
5024 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4944 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4944 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
5020 KB |
Output is correct |
9 |
Correct |
3 ms |
5020 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5024 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
5024 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4944 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4944 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
16 ms |
5324 KB |
Output is correct |
23 |
Correct |
17 ms |
5416 KB |
Output is correct |
24 |
Correct |
16 ms |
5388 KB |
Output is correct |
25 |
Correct |
16 ms |
5388 KB |
Output is correct |
26 |
Correct |
16 ms |
5292 KB |
Output is correct |
27 |
Correct |
20 ms |
5400 KB |
Output is correct |
28 |
Correct |
17 ms |
5404 KB |
Output is correct |
29 |
Correct |
17 ms |
5416 KB |
Output is correct |
30 |
Correct |
18 ms |
5456 KB |
Output is correct |
31 |
Correct |
17 ms |
5444 KB |
Output is correct |
32 |
Correct |
23 ms |
5708 KB |
Output is correct |
33 |
Correct |
20 ms |
5708 KB |
Output is correct |
34 |
Correct |
14 ms |
5836 KB |
Output is correct |
35 |
Correct |
14 ms |
5964 KB |
Output is correct |
36 |
Correct |
17 ms |
5520 KB |
Output is correct |
37 |
Correct |
14 ms |
5708 KB |
Output is correct |
38 |
Correct |
20 ms |
5836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
5020 KB |
Output is correct |
9 |
Correct |
3 ms |
5020 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5024 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
5024 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4944 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4944 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
16 ms |
5324 KB |
Output is correct |
23 |
Correct |
17 ms |
5416 KB |
Output is correct |
24 |
Correct |
16 ms |
5388 KB |
Output is correct |
25 |
Correct |
16 ms |
5388 KB |
Output is correct |
26 |
Correct |
16 ms |
5292 KB |
Output is correct |
27 |
Correct |
20 ms |
5400 KB |
Output is correct |
28 |
Correct |
17 ms |
5404 KB |
Output is correct |
29 |
Correct |
17 ms |
5416 KB |
Output is correct |
30 |
Correct |
18 ms |
5456 KB |
Output is correct |
31 |
Correct |
17 ms |
5444 KB |
Output is correct |
32 |
Correct |
23 ms |
5708 KB |
Output is correct |
33 |
Correct |
20 ms |
5708 KB |
Output is correct |
34 |
Correct |
14 ms |
5836 KB |
Output is correct |
35 |
Correct |
14 ms |
5964 KB |
Output is correct |
36 |
Correct |
17 ms |
5520 KB |
Output is correct |
37 |
Correct |
14 ms |
5708 KB |
Output is correct |
38 |
Correct |
20 ms |
5836 KB |
Output is correct |
39 |
Correct |
1147 ms |
21860 KB |
Output is correct |
40 |
Correct |
1104 ms |
21572 KB |
Output is correct |
41 |
Correct |
1142 ms |
21896 KB |
Output is correct |
42 |
Correct |
1099 ms |
22372 KB |
Output is correct |
43 |
Correct |
1148 ms |
22320 KB |
Output is correct |
44 |
Correct |
1135 ms |
22264 KB |
Output is correct |
45 |
Correct |
2139 ms |
38340 KB |
Output is correct |
46 |
Correct |
1890 ms |
44440 KB |
Output is correct |
47 |
Correct |
891 ms |
48040 KB |
Output is correct |
48 |
Correct |
806 ms |
56572 KB |
Output is correct |
49 |
Correct |
1403 ms |
28300 KB |
Output is correct |
50 |
Correct |
815 ms |
39456 KB |
Output is correct |
51 |
Correct |
1354 ms |
48000 KB |
Output is correct |