Submission #957712

# Submission time Handle Problem Language Result Execution time Memory
957712 2024-04-04T08:36:27 Z LCJLY Meetings 2 (JOI21_meetings2) C++14
100 / 100
812 ms 65600 KB
#include <bits/stdc++.h>
using namespace std;	
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

vector<int>adj[200005];
int sz[200005];
int two[22][200005];
int d[200005];
int n;

void dfs(int index, int par){
	sz[index]=1;
	for(int x=0;x<20;x++){
		two[x+1][index]=two[x][two[x][index]];
	}
	for(auto it:adj[index]){
		if(it==par) continue;
		two[0][it]=index;
		d[it]=d[index]+1;
		dfs(it,index);
		sz[index]+=sz[it];
	}
}

int cent(int index, int par){
	for(auto it:adj[index]){
		if(it!=par&&sz[it]>n/2){
			return cent(it,index);
		}
	}
	return index;
}

int lca(int a, int b){
	if(d[a]>d[b]) swap(a,b);
	int diff=d[b]-d[a];
	for(int x=0;x<20;x++){
		if(diff&(1<<x)){
			b=two[x][b];
		}	
	}
	
	if(a==b) return a;
	for(int x=19;x>=0;x--){
		if(two[x][a]!=two[x][b]){
			a=two[x][a];
			b=two[x][b];
		}
	}
	return two[0][a];
}

int f(int a, int b){
	int hold=lca(a,b);
	return d[a]+d[b]-2*d[hold];
}

void solve(){
	cin >> n;
	
	int temp,temp2;
	for(int x=0;x<n-1;x++){
		cin >> temp >> temp2;
		adj[temp].push_back(temp2);
		adj[temp2].push_back(temp);
	}
	
	dfs(1,-1);
	int rt=cent(1,-1);
	memset(two,0,sizeof(two));
	dfs(rt,-1);
	//show(check,1);
	
	int ans[n+5];
	memset(ans,0,sizeof(ans));
	pii cur={rt,rt};
	int dist=0;
	
	vector<pii>v;
	for(int x=1;x<=n;x++){
		v.push_back({sz[x],x});
	}
	
	sort(v.begin(),v.end());
	reverse(v.begin(),v.end());
	
	for(int x=0;x<n;x++){
		int index=v[x].second;
		int hold=f(cur.first,index);
		int hold2=f(cur.second,index);
		pii temp={cur.second,index};
		if(hold>dist){
			cur={cur.first,index};
			dist=hold;
		}
		if(hold2>dist){
			dist=hold2;
			cur=temp;
		}
		
		ans[sz[index]]=dist;
	}
	
	for(int x=n;x>=0;x--){
		ans[x]=max(ans[x+1],ans[x]);
	}
	
	for(int x=1;x<=n;x++){
		if(x%2){
			cout << 1 << "\n";
		}
		else{
			cout << ans[x/2]+1 << "\n";
		}
	}
	
}
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 41048 KB Output is correct
2 Correct 9 ms 41052 KB Output is correct
3 Correct 8 ms 41052 KB Output is correct
4 Correct 8 ms 41048 KB Output is correct
5 Correct 8 ms 41048 KB Output is correct
6 Correct 9 ms 41048 KB Output is correct
7 Correct 8 ms 41052 KB Output is correct
8 Correct 8 ms 41052 KB Output is correct
9 Correct 8 ms 41052 KB Output is correct
10 Correct 8 ms 41052 KB Output is correct
11 Correct 8 ms 41048 KB Output is correct
12 Correct 8 ms 41052 KB Output is correct
13 Correct 9 ms 41052 KB Output is correct
14 Correct 8 ms 41052 KB Output is correct
15 Correct 9 ms 41172 KB Output is correct
16 Correct 8 ms 41048 KB Output is correct
17 Correct 8 ms 41052 KB Output is correct
18 Correct 9 ms 41052 KB Output is correct
19 Correct 8 ms 41052 KB Output is correct
20 Correct 9 ms 41052 KB Output is correct
21 Correct 8 ms 41052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 41048 KB Output is correct
2 Correct 9 ms 41052 KB Output is correct
3 Correct 8 ms 41052 KB Output is correct
4 Correct 8 ms 41048 KB Output is correct
5 Correct 8 ms 41048 KB Output is correct
6 Correct 9 ms 41048 KB Output is correct
7 Correct 8 ms 41052 KB Output is correct
8 Correct 8 ms 41052 KB Output is correct
9 Correct 8 ms 41052 KB Output is correct
10 Correct 8 ms 41052 KB Output is correct
11 Correct 8 ms 41048 KB Output is correct
12 Correct 8 ms 41052 KB Output is correct
13 Correct 9 ms 41052 KB Output is correct
14 Correct 8 ms 41052 KB Output is correct
15 Correct 9 ms 41172 KB Output is correct
16 Correct 8 ms 41048 KB Output is correct
17 Correct 8 ms 41052 KB Output is correct
18 Correct 9 ms 41052 KB Output is correct
19 Correct 8 ms 41052 KB Output is correct
20 Correct 9 ms 41052 KB Output is correct
21 Correct 8 ms 41052 KB Output is correct
22 Correct 12 ms 41384 KB Output is correct
23 Correct 12 ms 41308 KB Output is correct
24 Correct 12 ms 41564 KB Output is correct
25 Correct 11 ms 41308 KB Output is correct
26 Correct 11 ms 41308 KB Output is correct
27 Correct 12 ms 41308 KB Output is correct
28 Correct 12 ms 41564 KB Output is correct
29 Correct 12 ms 41484 KB Output is correct
30 Correct 11 ms 41564 KB Output is correct
31 Correct 12 ms 41564 KB Output is correct
32 Correct 12 ms 41560 KB Output is correct
33 Correct 12 ms 41564 KB Output is correct
34 Correct 11 ms 41392 KB Output is correct
35 Correct 11 ms 41564 KB Output is correct
36 Correct 11 ms 41464 KB Output is correct
37 Correct 11 ms 41564 KB Output is correct
38 Correct 11 ms 41452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 41048 KB Output is correct
2 Correct 9 ms 41052 KB Output is correct
3 Correct 8 ms 41052 KB Output is correct
4 Correct 8 ms 41048 KB Output is correct
5 Correct 8 ms 41048 KB Output is correct
6 Correct 9 ms 41048 KB Output is correct
7 Correct 8 ms 41052 KB Output is correct
8 Correct 8 ms 41052 KB Output is correct
9 Correct 8 ms 41052 KB Output is correct
10 Correct 8 ms 41052 KB Output is correct
11 Correct 8 ms 41048 KB Output is correct
12 Correct 8 ms 41052 KB Output is correct
13 Correct 9 ms 41052 KB Output is correct
14 Correct 8 ms 41052 KB Output is correct
15 Correct 9 ms 41172 KB Output is correct
16 Correct 8 ms 41048 KB Output is correct
17 Correct 8 ms 41052 KB Output is correct
18 Correct 9 ms 41052 KB Output is correct
19 Correct 8 ms 41052 KB Output is correct
20 Correct 9 ms 41052 KB Output is correct
21 Correct 8 ms 41052 KB Output is correct
22 Correct 12 ms 41384 KB Output is correct
23 Correct 12 ms 41308 KB Output is correct
24 Correct 12 ms 41564 KB Output is correct
25 Correct 11 ms 41308 KB Output is correct
26 Correct 11 ms 41308 KB Output is correct
27 Correct 12 ms 41308 KB Output is correct
28 Correct 12 ms 41564 KB Output is correct
29 Correct 12 ms 41484 KB Output is correct
30 Correct 11 ms 41564 KB Output is correct
31 Correct 12 ms 41564 KB Output is correct
32 Correct 12 ms 41560 KB Output is correct
33 Correct 12 ms 41564 KB Output is correct
34 Correct 11 ms 41392 KB Output is correct
35 Correct 11 ms 41564 KB Output is correct
36 Correct 11 ms 41464 KB Output is correct
37 Correct 11 ms 41564 KB Output is correct
38 Correct 11 ms 41452 KB Output is correct
39 Correct 429 ms 60036 KB Output is correct
40 Correct 402 ms 58260 KB Output is correct
41 Correct 404 ms 59300 KB Output is correct
42 Correct 445 ms 59072 KB Output is correct
43 Correct 455 ms 58988 KB Output is correct
44 Correct 418 ms 60720 KB Output is correct
45 Correct 812 ms 62912 KB Output is correct
46 Correct 416 ms 65544 KB Output is correct
47 Correct 277 ms 59036 KB Output is correct
48 Correct 190 ms 58940 KB Output is correct
49 Correct 542 ms 59584 KB Output is correct
50 Correct 250 ms 59708 KB Output is correct
51 Correct 314 ms 65600 KB Output is correct