답안 #1014739

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1014739 2024-07-05T11:49:03 Z NintsiChkhaidze Meetings 2 (JOI21_meetings2) C++17
100 / 100
505 ms 42836 KB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
using namespace std;

const int N = 2e5 + 5;
vector <int> v[N];
int c = -1,ans[N],sz[N],dep[N],d[25][N];
pii arr[N];

int findcentroid(int x,int par,int n){
	int s = 1;
	for (int to: v[x]){
		if (to == par) continue;
		s += findcentroid(to,x,n);
	}

	if (c == -1 && (par == -1 || s * 2 >= n)) c = x;
	return s;
}

int lca(int x,int y){
	if (dep[x] < dep[y]) swap(x,y);
	for (int i = 20; i >= 0; i--)
		if (dep[d[i][x]] >= dep[y]) x = d[i][x];
	if (x == y) return x;
 
	for (int i = 20; i >= 0; i--)
		if (d[i][x] != d[i][y]) x = d[i][x],y = d[i][y];
 
	return d[0][x];
}

void dfs(int x,int par){
	sz[x] = 1;
	d[0][x] = par;
	dep[x] = dep[par] + 1;
	for (int to: v[x]){
		if (to == par) continue;
		dfs(to,x);
		sz[x] += sz[to];
	}
}

int getdistance(int x,int y){
	int c=lca(x,y);
	return dep[x]+dep[y]-2*dep[c]+1;
}

signed main() {
	ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);

	int n;
	cin>>n;

	for (int i = 1; i < n; i++){
		int a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}

	findcentroid(1,-1,n);
	dfs(c,c);
	for (int j = 1; j <= 20 ; j++)
		for (int i = 1; i <= n; i++)
			d[j][i] = d[j - 1][d[j - 1][i]];

	for (int i = 1; i <= n; i++)
		arr[i] = {sz[i],i};
	sort(arr+1,arr+n+1);

	int d1 = 0,d2 = 0,D=0,mx = 0;

	for (int i = n; i >= 1; i--){
		int x = arr[i].s;
		if (!d1) d1=d2=x,D=1;
		else{
			if (getdistance(d1,x) > D) {
				d2 = x;
				D = getdistance(d1,x);
			}

			if (getdistance(d2,x) > D){
				d1 = x;
				D = getdistance(d2,x);
			}
		}
		
		mx=max(mx,D);
		ans[arr[i].f] = mx;
	}

	for (int i = n; i >= 1; i--){
		ans[i] = max(ans[i],ans[i + 1]);
	}

	for (int i = 1; i <= n; i++){
		if (i&1) cout<<1<<endl;
		else cout<<ans[i/2]<<endl;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 4 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5160 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 2 ms 5292 KB Output is correct
15 Correct 3 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5160 KB Output is correct
18 Correct 2 ms 5160 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 4 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5160 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 2 ms 5292 KB Output is correct
15 Correct 3 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5160 KB Output is correct
18 Correct 2 ms 5160 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 11 ms 5872 KB Output is correct
23 Correct 8 ms 5864 KB Output is correct
24 Correct 9 ms 5864 KB Output is correct
25 Correct 9 ms 5724 KB Output is correct
26 Correct 8 ms 5724 KB Output is correct
27 Correct 9 ms 5688 KB Output is correct
28 Correct 9 ms 5884 KB Output is correct
29 Correct 11 ms 5724 KB Output is correct
30 Correct 8 ms 5724 KB Output is correct
31 Correct 9 ms 5860 KB Output is correct
32 Correct 9 ms 5724 KB Output is correct
33 Correct 8 ms 6036 KB Output is correct
34 Correct 8 ms 5724 KB Output is correct
35 Correct 8 ms 5776 KB Output is correct
36 Correct 8 ms 5724 KB Output is correct
37 Correct 10 ms 5720 KB Output is correct
38 Correct 8 ms 5940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 4 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 3 ms 5160 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 2 ms 5292 KB Output is correct
15 Correct 3 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5160 KB Output is correct
18 Correct 2 ms 5160 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 11 ms 5872 KB Output is correct
23 Correct 8 ms 5864 KB Output is correct
24 Correct 9 ms 5864 KB Output is correct
25 Correct 9 ms 5724 KB Output is correct
26 Correct 8 ms 5724 KB Output is correct
27 Correct 9 ms 5688 KB Output is correct
28 Correct 9 ms 5884 KB Output is correct
29 Correct 11 ms 5724 KB Output is correct
30 Correct 8 ms 5724 KB Output is correct
31 Correct 9 ms 5860 KB Output is correct
32 Correct 9 ms 5724 KB Output is correct
33 Correct 8 ms 6036 KB Output is correct
34 Correct 8 ms 5724 KB Output is correct
35 Correct 8 ms 5776 KB Output is correct
36 Correct 8 ms 5724 KB Output is correct
37 Correct 10 ms 5720 KB Output is correct
38 Correct 8 ms 5940 KB Output is correct
39 Correct 377 ms 34388 KB Output is correct
40 Correct 355 ms 33788 KB Output is correct
41 Correct 352 ms 34340 KB Output is correct
42 Correct 385 ms 34896 KB Output is correct
43 Correct 368 ms 34756 KB Output is correct
44 Correct 380 ms 34900 KB Output is correct
45 Correct 505 ms 39184 KB Output is correct
46 Correct 411 ms 42836 KB Output is correct
47 Correct 335 ms 35272 KB Output is correct
48 Correct 269 ms 35380 KB Output is correct
49 Correct 397 ms 35412 KB Output is correct
50 Correct 326 ms 35272 KB Output is correct
51 Correct 352 ms 41168 KB Output is correct