답안 #523335

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
523335 2022-02-07T13:25:00 Z amunduzbaev Meetings 2 (JOI21_meetings2) C++17
100 / 100
434 ms 52428 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
 
const int N = 2e5 + 5;
vector<int> edges[N], cnt[N];
int sub[N], par[N][20], d[N];
int tin[N], tout[N], t;

void dfs(int u, int& C, int n, int p = -1){
	tin[u] = ++t, sub[u] = 1;
	for(int i=1;i<20;i++) par[u][i] = par[par[u][i-1]][i-1];
	for(auto x : edges[u]){
		if(x == p) continue;
		d[x] = d[u] + 1;
		par[x][0] = u;
		dfs(x, C, n, u);
		sub[u] += sub[x];
	}
	if(sub[u] > n / 2 && C == -1) C = u;
	tout[u] = t;
}

bool is(int a, int b){ return (tin[a] <= tin[b] && tout[b] <= tout[a]); }

int dis(int a, int b){
	//~ cout<<a<<" "<<b<<endl;
	if(is(a, b)) return d[b] - d[a];
	if(is(b, a)) return d[a] - d[b];
	int D = d[a] + d[b];
	for(int j=19;~j;j--){
		if(!is(par[a][j], b)) a = par[a][j];
	} a = par[a][0];
	return D - d[a] * 2;
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n; cin>>n;
	for(int i=1;i<n;i++){
		int a, b; cin>>a>>b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	
	int C = -1;
	par[1][0] = 1;
	dfs(1, C, n);
	
	vector<int> rr(n + 1);
	int a = C, b = C, d = 0;
	//~ cout<<C<<endl;
	par[C][0] = C;
	dfs(C, C, n);
	for(int i=1;i<=n;i++){
		cnt[sub[i]].push_back(i);
	}
	
	for(int i=n/2;i>0;i--){
		for(auto x : cnt[i]){
			//~ cout<<x<<" "<<a<<" "<<b<<" "<<d<<endl;;
			int da = dis(a, x), db = dis(b, x);
			if(da < db) swap(da, db), swap(a, b);
			
			if(da > d) d = da, b = x;
			else if(db > d) d = db, a = x;
		}
		
		rr[i] = d + 1;
	}
	
	for(int i=1;i<=n;i++){
		if(i&1) cout<<1<<"\n";
		else cout<<rr[i/2]<<"\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9692 KB Output is correct
2 Correct 5 ms 9704 KB Output is correct
3 Correct 6 ms 9736 KB Output is correct
4 Correct 5 ms 9712 KB Output is correct
5 Correct 5 ms 9696 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9708 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9676 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9688 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 7 ms 9708 KB Output is correct
17 Correct 5 ms 9640 KB Output is correct
18 Correct 5 ms 9708 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 5 ms 9716 KB Output is correct
21 Correct 5 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9692 KB Output is correct
2 Correct 5 ms 9704 KB Output is correct
3 Correct 6 ms 9736 KB Output is correct
4 Correct 5 ms 9712 KB Output is correct
5 Correct 5 ms 9696 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9708 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9676 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9688 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 7 ms 9708 KB Output is correct
17 Correct 5 ms 9640 KB Output is correct
18 Correct 5 ms 9708 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 5 ms 9716 KB Output is correct
21 Correct 5 ms 9676 KB Output is correct
22 Correct 8 ms 10316 KB Output is correct
23 Correct 9 ms 10232 KB Output is correct
24 Correct 10 ms 10236 KB Output is correct
25 Correct 8 ms 10316 KB Output is correct
26 Correct 8 ms 10316 KB Output is correct
27 Correct 8 ms 10316 KB Output is correct
28 Correct 9 ms 10316 KB Output is correct
29 Correct 10 ms 10216 KB Output is correct
30 Correct 11 ms 10316 KB Output is correct
31 Correct 8 ms 10316 KB Output is correct
32 Correct 9 ms 10500 KB Output is correct
33 Correct 8 ms 10492 KB Output is correct
34 Correct 9 ms 10288 KB Output is correct
35 Correct 8 ms 10272 KB Output is correct
36 Correct 8 ms 10316 KB Output is correct
37 Correct 8 ms 10232 KB Output is correct
38 Correct 8 ms 10360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9692 KB Output is correct
2 Correct 5 ms 9704 KB Output is correct
3 Correct 6 ms 9736 KB Output is correct
4 Correct 5 ms 9712 KB Output is correct
5 Correct 5 ms 9696 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9708 KB Output is correct
9 Correct 5 ms 9676 KB Output is correct
10 Correct 5 ms 9676 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9688 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 7 ms 9708 KB Output is correct
17 Correct 5 ms 9640 KB Output is correct
18 Correct 5 ms 9708 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 5 ms 9716 KB Output is correct
21 Correct 5 ms 9676 KB Output is correct
22 Correct 8 ms 10316 KB Output is correct
23 Correct 9 ms 10232 KB Output is correct
24 Correct 10 ms 10236 KB Output is correct
25 Correct 8 ms 10316 KB Output is correct
26 Correct 8 ms 10316 KB Output is correct
27 Correct 8 ms 10316 KB Output is correct
28 Correct 9 ms 10316 KB Output is correct
29 Correct 10 ms 10216 KB Output is correct
30 Correct 11 ms 10316 KB Output is correct
31 Correct 8 ms 10316 KB Output is correct
32 Correct 9 ms 10500 KB Output is correct
33 Correct 8 ms 10492 KB Output is correct
34 Correct 9 ms 10288 KB Output is correct
35 Correct 8 ms 10272 KB Output is correct
36 Correct 8 ms 10316 KB Output is correct
37 Correct 8 ms 10232 KB Output is correct
38 Correct 8 ms 10360 KB Output is correct
39 Correct 290 ms 39076 KB Output is correct
40 Correct 263 ms 38284 KB Output is correct
41 Correct 308 ms 39236 KB Output is correct
42 Correct 277 ms 39848 KB Output is correct
43 Correct 326 ms 39648 KB Output is correct
44 Correct 300 ms 39776 KB Output is correct
45 Correct 434 ms 47128 KB Output is correct
46 Correct 392 ms 52428 KB Output is correct
47 Correct 271 ms 40112 KB Output is correct
48 Correct 197 ms 40300 KB Output is correct
49 Correct 270 ms 40420 KB Output is correct
50 Correct 201 ms 40124 KB Output is correct
51 Correct 277 ms 50128 KB Output is correct