This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define ll long long
const int nmax = 2e5 + 5, N = 3e5 + 5;
const ll oo = 1e18;
const int lg = 18, M = 4e3;
const int base = 2e5, mod = 998244353;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;
int n, a[nmax];
vector<int> adj[nmax];
namespace sub1{
int sz[nmax], root, cntw;
int h[nmax];
void dfs_1(int u, int p){
sz[u] = 1;
for(auto v : adj[u]){
if(v == p) continue;
dfs_1(v, u);
sz[u] += sz[v];
}
}
void dfs_2(int u, int p){
for(auto v : adj[u]){
if(v == p) continue;
if(sz[v] > cntw/2) return dfs_2(v, u);
}
root = u;
}
int ans[nmax], up[nmax][lg + 1];
void dfs(int u, int p){
sz[u] = 1;
for(auto v : adj[u]){
if(v == p) continue;
h[v] = h[u] + 1;
up[v][0] = u;
for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1];
dfs(v, u);
sz[u] += sz[v];
}
}
int lca(int u, int v){
if(h[u] != h[v]){
if(h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
for(int j = 0; j <= lg; ++j){
if(k >> j & 1){
u = up[u][j];
}
}
}
if(u == v) return u;
for(int j = __lg(h[u]); j >= 0; --j){
if(up[u][j] != up[v][j]){
u = up[u][j];
v = up[v][j];
}
}
return up[u][0];
}
int dist(int u, int v){
int x = lca(u, v);
return h[u] + h[v] - 2* h[x];
}
void sol(){
dfs_1(1, -1), cntw =sz[1];
dfs_2(1, -1);
dfs(root, -1);
vector<int> idx;
for(int i = 1; i <= n; ++i){
// if(i != root){
idx.push_back(i);
// }
}
sort(idx.begin(), idx.end(), [](int a, int b){
return sz[a] > sz[b];
});
int pos = 0;
int u = -1, v = -1;
// cout << root << '*';
for(int i = n; i >= 1; --i){
if(i & 1);
else{
while(pos < idx.size() && sz[idx[pos]] >= i / 2){
int x = idx[pos];
if(u == -1) u = v = x;
else{
a[1] = x, a[2] = u, a[3] = v;
int ma = 0;
for(int i = 1; i <= 3; ++i){
for(int j = i; j <= 3; ++j){
int cur =dist(a[i], a[j]);
if(cur > ma){
u = a[i];
v = a[j];
ma = cur;
}
}
}
}
++pos;
}
if(u != -1){
// cout << i << ' ' << u << ' ' << v << endl;
ans[i / 2] = max(ans[i / 2], dist(u, v));
// cout << i << ' ' << u << ' ' << v << endl;
}
}
}
for(int i = 1; i <= n; ++i){
if(i & 1) cout << 1;
else cout << ans[i / 2] + 1;
cout << endl;
}
}
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
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);
}
sub1::sol();
}
/*
5
5 4
2 5
1 5
3 5
*/
Compilation message (stderr)
meetings2.cpp: In function 'void sub1::sol()':
meetings2.cpp:90:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | while(pos < idx.size() && sz[idx[pos]] >= i / 2){
| ~~~~^~~~~~~~~~~~
meetings2.cpp: At global scope:
meetings2.cpp:124:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
124 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |