Submission #543471

# Submission time Handle Problem Language Result Execution time Memory
543471 2022-03-30T16:56:01 Z alvingogo Meetings 2 (JOI21_meetings2) C++14
100 / 100
584 ms 37020 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define cd complex<double>
#define p_q priority_queue
using namespace std;

struct no{
	vector<int> ch;
	int as[20]={0};
	int dep=-1;
	int sz;
};
vector<no> v;
void dfs(int r,int f){
	v[r].dep=v[f].dep+1;
	v[r].as[0]=f;
	v[r].sz=1;
	for(auto h:v[r].ch){
		if(h!=f){
			dfs(h,r);
			v[r].sz+=v[h].sz;
		}
	}
}
void gas(int n){
	for(int i=1;i<20;i++){
		for(int j=0;j<n;j++){
			v[j].as[i]=v[v[j].as[i-1]].as[i-1];
		}
	}
}
int fc(int r,int f,int nz){
	for(auto h:v[r].ch){
		if(h!=f){
			if(v[h].sz>nz/2){
				return fc(h,r,nz);
			}
		}
	}
	return r;
}
int dia=0,cz,e=0,f=0;
int lca(int x,int y){
	if(v[x].dep<v[y].dep){
		swap(x,y);
	}
	for(int i=19;i>=0;i--){
		if(v[v[x].as[i]].dep>=v[y].dep){
			x=v[x].as[i];
		}
	}
	if(x==y){
		return x;
	}
	for(int i=19;i>=0;i--){
		if(v[x].as[i]!=v[y].as[i]){
			x=v[x].as[i];
			y=v[y].as[i];
		}	
	}
	return v[x].as[0];
}
int dis(int x,int y){
	int u=lca(x,y);
	return v[x].dep+v[y].dep-2*v[u].dep;
}
void add(int z){
	int x=dis(z,e);
	int y=dis(z,f);
	int a,b;
	if(x>y){
		a=e;
		b=x;
	}
	else{
		a=f;
		b=y;
	}
	if(b>dia){
		dia=b;
		if(e==a){
			f=z;
		}
		else{
			e=z;
		}
	}
}
int main(){
	AquA;
	int n;
	cin >> n;
	v.resize(n);
	for(int i=1;i<n;i++){
		int a,b;
		cin >> a >> b;
		a--;
		b--;
		v[a].ch.push_back(b);
		v[b].ch.push_back(a);
	}
	dfs(0,0);
	int ct=fc(0,0,n);
	dfs(ct,ct);
	gas(n);
	vector<pair<int,int> > vp;
	for(int i=0;i<n;i++){
		vp.push_back({v[i].sz,i});
	}	
	sort(vp.begin(),vp.end(),greater<pair<int,int> >());
	vector<int> ans(n+1);
	e=ct;
	f=ct;
	cz=1;
	for(int i=1;i<=n;i+=2){
		ans[i]=1;
	}
	for(int i=n-n%2;i>=2;i-=2){
		while(cz<n){
			if(vp[cz].fs<i/2){
				break;
			}
			add(vp[cz].sc);
			cz++;
		}
		ans[i]=dia+1;
	}
	for(int i=1;i<=n;i++){
		cout << ans[i] << "\n";
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 316 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 316 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 5 ms 980 KB Output is correct
23 Correct 6 ms 980 KB Output is correct
24 Correct 5 ms 980 KB Output is correct
25 Correct 6 ms 980 KB Output is correct
26 Correct 6 ms 896 KB Output is correct
27 Correct 5 ms 980 KB Output is correct
28 Correct 5 ms 980 KB Output is correct
29 Correct 4 ms 980 KB Output is correct
30 Correct 5 ms 968 KB Output is correct
31 Correct 5 ms 976 KB Output is correct
32 Correct 5 ms 968 KB Output is correct
33 Correct 5 ms 980 KB Output is correct
34 Correct 4 ms 980 KB Output is correct
35 Correct 4 ms 976 KB Output is correct
36 Correct 5 ms 980 KB Output is correct
37 Correct 5 ms 980 KB Output is correct
38 Correct 4 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 316 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 5 ms 980 KB Output is correct
23 Correct 6 ms 980 KB Output is correct
24 Correct 5 ms 980 KB Output is correct
25 Correct 6 ms 980 KB Output is correct
26 Correct 6 ms 896 KB Output is correct
27 Correct 5 ms 980 KB Output is correct
28 Correct 5 ms 980 KB Output is correct
29 Correct 4 ms 980 KB Output is correct
30 Correct 5 ms 968 KB Output is correct
31 Correct 5 ms 976 KB Output is correct
32 Correct 5 ms 968 KB Output is correct
33 Correct 5 ms 980 KB Output is correct
34 Correct 4 ms 980 KB Output is correct
35 Correct 4 ms 976 KB Output is correct
36 Correct 5 ms 980 KB Output is correct
37 Correct 5 ms 980 KB Output is correct
38 Correct 4 ms 980 KB Output is correct
39 Correct 350 ms 32552 KB Output is correct
40 Correct 317 ms 31860 KB Output is correct
41 Correct 352 ms 32616 KB Output is correct
42 Correct 343 ms 33168 KB Output is correct
43 Correct 363 ms 33184 KB Output is correct
44 Correct 333 ms 33280 KB Output is correct
45 Correct 584 ms 35340 KB Output is correct
46 Correct 463 ms 37020 KB Output is correct
47 Correct 287 ms 33472 KB Output is correct
48 Correct 248 ms 33620 KB Output is correct
49 Correct 370 ms 33660 KB Output is correct
50 Correct 273 ms 33728 KB Output is correct
51 Correct 337 ms 36668 KB Output is correct