답안 #884685

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
884685 2023-12-08T04:34:05 Z willychan Meetings 2 (JOI21_meetings2) C++17
100 / 100
275 ms 42444 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds

const int N = 2e5+5;
vector<int> side[N];
int sz[N];
int dep[N];
int P[N][20];
void dfs(int cur,int p){
	sz[cur]=1;
	for(auto i : side[cur]){
		if(i!=p){
			dfs(i,cur);
			sz[cur]+=sz[i];
		}
	}
}

void built(int cur,int p){
	P[cur][0]=p;
	for(auto i : side[cur])	{
		if(i!=p){
			dep[i] = dep[cur]+1;
			built(i,cur);
		}
	}
}

int n;
int centroid(int cur,int p){
	for(auto i : side[cur]){
		if(i==p) continue;
		if(sz[i]>n/2) return centroid(i,cur);
	}
	return cur;
}

int dis(int a,int b){
	if(dep[a]>dep[b]) swap(a,b);
	int oga = a;
	int ogb = b;
	if(dep[a]!=dep[b]){
		for(int i=19;i>=0;i--){
			if(dep[P[b][i]]>dep[a]) b = P[b][i];
		}
		b = P[b][0];
	}
	assert(dep[a]==dep[b]);
	int c = 0;
	if(a==b) c = a;
	else{
		for(int i=19;i>=0;i--){
			if(P[a][i]!=P[b][i]) {
				a = P[a][i];
				b = P[b][i];
			}
		}
		c = P[a][0];
	}
	return dep[oga]+dep[ogb]-2*dep[c]+1;
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=0;i<n-1;i++){
		int a,b;cin>>a>>b;
		side[a].push_back(b);
		side[b].push_back(a);
	}
	dfs(1,0);
	int cen = centroid(1,0);
	dfs(cen,0);
	vector<int> ord;	
	for(int i=1;i<=n;i++) ord.push_back(i);
	sort(ord.begin(),ord.end(),[&](const int &a,const int &b){return sz[a]>sz[b];});
	built(cen,0);	
	P[cen][0]=cen;
	for(int j=1;j<20;j++){
		for(int i=1;i<=n;i++) P[i][j] = P[P[i][j-1]][j-1];
	}
	vector<int> ans(n+1,0);
	int dia = 1;
	int one = cen;
	int two = -1;
	int head = 1;
	for(int i=(n/2+1);i>=0;i--){
		while(head<n && sz[ord[head]]>=i){
			int a = ord[head];
			if(two==-1){
				two = a;
				dia = max(dia,dis(a,one));
			}else{
				int dis1 = dis(a,one);
				int dis2 = dis(a,two);
				if(dis1>dia){
					two = a;
					dia= dis1;
				}else if(dis2>dia){
					one = a;
					dia = dis2;
				}
			}
			head++;
		}
		ans[i] = dia;
	}
	for(int i=1;i<=n;i++){
		if(i&1)	 cout<<1<<"\n";
		else cout<<ans[i/2]<<"\n";
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7772 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 2 ms 7772 KB Output is correct
7 Correct 2 ms 7768 KB Output is correct
8 Correct 2 ms 7768 KB Output is correct
9 Correct 2 ms 7772 KB Output is correct
10 Correct 2 ms 7768 KB Output is correct
11 Correct 2 ms 7772 KB Output is correct
12 Correct 2 ms 7772 KB Output is correct
13 Correct 2 ms 7772 KB Output is correct
14 Correct 2 ms 7772 KB Output is correct
15 Correct 3 ms 7836 KB Output is correct
16 Correct 2 ms 7772 KB Output is correct
17 Correct 2 ms 7840 KB Output is correct
18 Correct 2 ms 7772 KB Output is correct
19 Correct 2 ms 7772 KB Output is correct
20 Correct 2 ms 7832 KB Output is correct
21 Correct 2 ms 7832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7772 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 2 ms 7772 KB Output is correct
7 Correct 2 ms 7768 KB Output is correct
8 Correct 2 ms 7768 KB Output is correct
9 Correct 2 ms 7772 KB Output is correct
10 Correct 2 ms 7768 KB Output is correct
11 Correct 2 ms 7772 KB Output is correct
12 Correct 2 ms 7772 KB Output is correct
13 Correct 2 ms 7772 KB Output is correct
14 Correct 2 ms 7772 KB Output is correct
15 Correct 3 ms 7836 KB Output is correct
16 Correct 2 ms 7772 KB Output is correct
17 Correct 2 ms 7840 KB Output is correct
18 Correct 2 ms 7772 KB Output is correct
19 Correct 2 ms 7772 KB Output is correct
20 Correct 2 ms 7832 KB Output is correct
21 Correct 2 ms 7832 KB Output is correct
22 Correct 4 ms 10076 KB Output is correct
23 Correct 5 ms 10080 KB Output is correct
24 Correct 5 ms 10228 KB Output is correct
25 Correct 4 ms 10084 KB Output is correct
26 Correct 5 ms 10236 KB Output is correct
27 Correct 5 ms 10084 KB Output is correct
28 Correct 5 ms 10080 KB Output is correct
29 Correct 6 ms 10152 KB Output is correct
30 Correct 4 ms 10080 KB Output is correct
31 Correct 5 ms 10076 KB Output is correct
32 Correct 5 ms 10376 KB Output is correct
33 Correct 5 ms 10336 KB Output is correct
34 Correct 4 ms 10076 KB Output is correct
35 Correct 3 ms 10292 KB Output is correct
36 Correct 5 ms 10076 KB Output is correct
37 Correct 4 ms 10076 KB Output is correct
38 Correct 4 ms 10332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7772 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 2 ms 7772 KB Output is correct
7 Correct 2 ms 7768 KB Output is correct
8 Correct 2 ms 7768 KB Output is correct
9 Correct 2 ms 7772 KB Output is correct
10 Correct 2 ms 7768 KB Output is correct
11 Correct 2 ms 7772 KB Output is correct
12 Correct 2 ms 7772 KB Output is correct
13 Correct 2 ms 7772 KB Output is correct
14 Correct 2 ms 7772 KB Output is correct
15 Correct 3 ms 7836 KB Output is correct
16 Correct 2 ms 7772 KB Output is correct
17 Correct 2 ms 7840 KB Output is correct
18 Correct 2 ms 7772 KB Output is correct
19 Correct 2 ms 7772 KB Output is correct
20 Correct 2 ms 7832 KB Output is correct
21 Correct 2 ms 7832 KB Output is correct
22 Correct 4 ms 10076 KB Output is correct
23 Correct 5 ms 10080 KB Output is correct
24 Correct 5 ms 10228 KB Output is correct
25 Correct 4 ms 10084 KB Output is correct
26 Correct 5 ms 10236 KB Output is correct
27 Correct 5 ms 10084 KB Output is correct
28 Correct 5 ms 10080 KB Output is correct
29 Correct 6 ms 10152 KB Output is correct
30 Correct 4 ms 10080 KB Output is correct
31 Correct 5 ms 10076 KB Output is correct
32 Correct 5 ms 10376 KB Output is correct
33 Correct 5 ms 10336 KB Output is correct
34 Correct 4 ms 10076 KB Output is correct
35 Correct 3 ms 10292 KB Output is correct
36 Correct 5 ms 10076 KB Output is correct
37 Correct 4 ms 10076 KB Output is correct
38 Correct 4 ms 10332 KB Output is correct
39 Correct 181 ms 33148 KB Output is correct
40 Correct 182 ms 33044 KB Output is correct
41 Correct 181 ms 33132 KB Output is correct
42 Correct 178 ms 33484 KB Output is correct
43 Correct 182 ms 33480 KB Output is correct
44 Correct 180 ms 33336 KB Output is correct
45 Correct 275 ms 38316 KB Output is correct
46 Correct 215 ms 42444 KB Output is correct
47 Correct 151 ms 33604 KB Output is correct
48 Correct 91 ms 33860 KB Output is correct
49 Correct 220 ms 34080 KB Output is correct
50 Correct 135 ms 33900 KB Output is correct
51 Correct 174 ms 41288 KB Output is correct