#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});
ckmax(ans[2*tmpsiz[v]], 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> 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(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);
}
decomp(0, -1);
for(int i = n; i>=1; 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 |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5020 KB |
Output is correct |
4 |
Correct |
4 ms |
4944 KB |
Output is correct |
5 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5020 KB |
Output is correct |
4 |
Correct |
4 ms |
4944 KB |
Output is correct |
5 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5020 KB |
Output is correct |
4 |
Correct |
4 ms |
4944 KB |
Output is correct |
5 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |