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 mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 1987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<db, db> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };
Vi v[200001];
VPi vv[200001];
int depth[200001],parent[200001][18];
int cnt[200001];
void dfs(int N,int p=-1){
parent[N][0]=p;
cnt[N]=1;
for(int next:v[N]){
if(next==p)continue;
depth[next]=depth[N]+1;
dfs(next,N);
cnt[N]+=cnt[next];
}
}
int LCA(int x, int y) {
if (depth[x] < depth[y])swap(x, y);
int dif = depth[x] - depth[y];
for (int j = 0; dif > 0; j++) {
if (dif & 1)x = parent[x][j];
dif >>= 1;
}
if (x != y) {
fdn(j,16,0,1) {
if (parent[x][j] != -1 && parent[x][j] != parent[y][j]) {
x = parent[x][j];
y = parent[y][j];
}
}
x = parent[x][0];
}
return x;
}
int dist(int x,int y){
int u=LCA(x,y);
return depth[x]+depth[y]-2*depth[u];
}
int cc,n;
void dfs2(int N,int p=-1){
for(int next:v[N]){
if(next==p)continue;
vv[min(cnt[next],n-cnt[next])].pb(mp(N,next));
dfs2(next,N);
}
}
int ans[200001];
bool chk[200001];
int upd(int x,int &sz,int &a,int &b){
chk[x]=1;
sz++;
if(sz==1){
a=x;
return 1;
}else if(sz==2){
b=x;
return 2;
}else{
int d1=dist(a,b),d2=dist(a,x),d3=dist(b,x);
int res=max({d1,d2,d3});
if(d2==res)b=x;
else if(d3==res)a=x;
return res+1;
}
}
int main(){
MEM_1(parent);
scanf("%d",&n);
fup(i,1,n-1,1){
int x,y;
scanf("%d%d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
dfs(1);
fup(j,0,16,1)
fup(i,1,n,1)
if (parent[i][j] != -1)
parent[i][j + 1] = parent[parent[i][j]][j];
dfs2(1);
int sz=0,r=1;
int a=-1,b=-1;
fdn(i,n,1,1){
if(i&1)ans[i]=1;
else{
for(Pi P:vv[i/2]){
if(!chk[P.X])r=upd(P.X,sz,a,b);
if(!chk[P.Y])r=upd(P.Y,sz,a,b);
}
ans[i]=r;
}
}
fup(i,1,n,1)printf("%d\n",ans[i]);
}
Compilation message (stderr)
meetings2.cpp: In function 'int LCA(int, int)':
meetings2.cpp:11:30: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
| ^
meetings2.cpp:66:3: note: in expansion of macro 'fdn'
66 | fdn(j,16,0,1) {
| ^~~
meetings2.cpp: In function 'int main()':
meetings2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
meetings2.cpp:110:2: note: in expansion of macro 'fup'
110 | fup(i,1,n-1,1){
| ^~~
meetings2.cpp:10:30: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
meetings2.cpp:117:2: note: in expansion of macro 'fup'
117 | fup(j,0,16,1)
| ^~~
meetings2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
meetings2.cpp:118:3: note: in expansion of macro 'fup'
118 | fup(i,1,n,1)
| ^~~
meetings2.cpp:11:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
| ^
meetings2.cpp:124:2: note: in expansion of macro 'fdn'
124 | fdn(i,n,1,1){
| ^~~
meetings2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
meetings2.cpp:134:2: note: in expansion of macro 'fup'
134 | fup(i,1,n,1)printf("%d\n",ans[i]);
| ^~~
meetings2.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
meetings2.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |