#include "bits/stdc++.h"
using namespace std;
const int N=2e5+1;
int n;
vector<int> adj[N];
int ans[N];
bool vis[N];
int sz[N];
void dfs1(int x, vector<int> &nodes){
vis[x]=1;
nodes.push_back(x);
sz[x]=1;
for(int k:adj[x]){
if(vis[k]) continue;
dfs1(k, nodes);
sz[x]+=sz[k];
}
}
void dfs2(int x, int cnt, int &c){
vis[x]=1;
for(int k:adj[x]){
if(vis[k]) continue;
if(sz[k]>cnt/2){
dfs2(k, cnt, c);
if(c) return;
}
}
if(!c) c=x;
}
void dfs3(int x, vector<int>& dp, int d){
sz[x]=1;
vis[x]=1;
for(int k:adj[x]){
if(vis[k]) continue;
dfs3(k, dp, d+1);
sz[x]+=sz[k];
}
if(dp.size()<=sz[x]) dp.resize(sz[x]+1);
// cout << x << " dp[" << sz[x] << "] max= " << d << "\n";
dp[sz[x]]=max(dp[sz[x]], d);
}
void go(int x){
// cout << "go " << x << "\n";
vector<int> nodes;
dfs1(x, nodes);
if(nodes.size()<=2) return;
for(int u:nodes) vis[u]=0;
int c=0;
dfs2(x, nodes.size(), c);
// cout << "nodes ";
// for(int x:nodes) cout << x << vis[x] << " ";
// cout << "\ncentroid: " << c << "\n";
for(int u:nodes) if(u!=c) vis[u]=0;
for(int k:adj[c]) if(!vis[k]) go(k);
vector<int> mx(nodes.size(), -1), mx2(nodes.size(), -1);
for(int u:nodes) if(u!=c) vis[u]=0;
for(int k:adj[c]){
if(vis[k]) continue;
vector<int> dp;
dfs3(k, dp, 1);
dp.push_back(0);
for(int i=dp.size()-1; i; i--){
if(i!=dp.size()-1) dp[i]=max(dp[i], dp[i+1]);
if(dp[i]>mx[i]) mx2[i]=mx[i], mx[i]=dp[i];
else if(dp[i]>mx2[i]) mx2[i]=dp[i];
// cout << mx[i] << mx2[i] << "\n";
}
}
for(int i=1; i<min(n/2+1, (int)nodes.size()); i++){
if(mx[i]!=-1 && mx2[i]!=-1) ans[2*i]=max(ans[2*i], mx[i]+mx2[i]);
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1; i<n; i++){
int u,v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
go(1);
for(int i=1; i<=n; i++){
if(i&1) cout << "1\n";
else cout << max(2, ans[i]+1) << "\n";
}
}
Compilation message
meetings2.cpp: In function 'void dfs3(int, std::vector<int>&, int)':
meetings2.cpp:42:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
42 | if(dp.size()<=sz[x]) dp.resize(sz[x]+1);
| ~~~~~~~~~^~~~~~~
meetings2.cpp: In function 'void go(int)':
meetings2.cpp:73:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | if(i!=dp.size()-1) dp[i]=max(dp[i], dp[i+1]);
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4960 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 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4960 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 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4960 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 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |