Submission #543270

# Submission time Handle Problem Language Result Execution time Memory
543270 2022-03-30T02:41:08 Z LittleCube Meetings 2 (JOI21_meetings2) C++14
100 / 100
816 ms 29884 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

int N, ans[200005], sz[200005], dis[200005], vis[2000005], pre[200005];
vector<int> E[200005], cur;
pii res[200005][2];

void update(int i, pii p)
{
	if(p.S == res[i][0].S)
		res[i][0].F = max(res[i][0].F, p.F);
	else if(p.S == res[i][1].S)
		res[i][1].F = max(res[i][1].F, p.F);
	else if(p.F >= res[i][0].F)
	{
		res[i][1] = res[i][0];
		res[i][0] = p;
	}
	else if(p.F >= res[i][1].F)
		res[i][1] = p;
	if(res[i][0].F < res[i][1].F)
		swap(res[i][0], res[i][1]);
}

void dfs(int k, int r)
{
	cur.emplace_back(k);
	vis[k] = 1;
	sz[k] = 1;
	for(int i : E[k])
		if(!vis[i])
		{
			pre[i] = k;
			dis[i] = dis[k] + 1;
			dfs(i, r);
			sz[k] += sz[i];
		}
	vis[k] = 0;
	update(sz[k], {dis[k], r});
}

void cdec(int k)
{
	cur.clear();
	pre[k] = 0;
	dfs(k, k);
	int c = 0, ccnt = 0, M = sz[k];
	for(int i : cur)
	{
		bool valid = (M - sz[i] <= M / 2);
		for(int j : E[i])
			if(j != pre[i] && !vis[j])
				valid &= (sz[j] <= M / 2);
			
		if(valid)
			c = i, ccnt++;
	}
	if(ccnt == 2)
		ans[M] = max(2, ans[M]);

	assert(ccnt <= 2);

	cur.clear();
	for(int i = 1; i <= M / 2; i++)
		res[i][0] = res[i][1] = {0, c};

	vis[c] = 1;
	for(int i : E[c]) 
		if(!vis[i])
		{
			dis[i] = 1;
			dfs(i, i);
		}

	for(int i = M / 2; i >= 1; i--)
	{
		ans[i * 2] = max(ans[i * 2], res[i][0].F + res[i][1].F + 1);
		update(i - 1, res[i][0]);
		update(i - 1, res[i][1]);
	}

	for(int i : E[c])
		if(!vis[i])
			cdec(i);
}



signed main()
{
	cin >> N;
	for(int i = 1; i < N; i++)
	{
		int u, v;
		cin >> u >> v;
		E[u].emplace_back(v);
		E[v].emplace_back(u);
	}
	cdec(1);
	for(int i = 1; i <= N; i++)
		cout << max(1, ans[i]) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 3 ms 5012 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 3 ms 5016 KB Output is correct
20 Correct 4 ms 5012 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 3 ms 5012 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 3 ms 5016 KB Output is correct
20 Correct 4 ms 5012 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 8 ms 5332 KB Output is correct
23 Correct 9 ms 5332 KB Output is correct
24 Correct 8 ms 5276 KB Output is correct
25 Correct 8 ms 5360 KB Output is correct
26 Correct 7 ms 5280 KB Output is correct
27 Correct 7 ms 5372 KB Output is correct
28 Correct 7 ms 5280 KB Output is correct
29 Correct 7 ms 5332 KB Output is correct
30 Correct 7 ms 5332 KB Output is correct
31 Correct 9 ms 5348 KB Output is correct
32 Correct 9 ms 5412 KB Output is correct
33 Correct 9 ms 5460 KB Output is correct
34 Correct 6 ms 5276 KB Output is correct
35 Correct 6 ms 5276 KB Output is correct
36 Correct 8 ms 5332 KB Output is correct
37 Correct 6 ms 5480 KB Output is correct
38 Correct 7 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 3 ms 5012 KB Output is correct
18 Correct 3 ms 5008 KB Output is correct
19 Correct 3 ms 5016 KB Output is correct
20 Correct 4 ms 5012 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 8 ms 5332 KB Output is correct
23 Correct 9 ms 5332 KB Output is correct
24 Correct 8 ms 5276 KB Output is correct
25 Correct 8 ms 5360 KB Output is correct
26 Correct 7 ms 5280 KB Output is correct
27 Correct 7 ms 5372 KB Output is correct
28 Correct 7 ms 5280 KB Output is correct
29 Correct 7 ms 5332 KB Output is correct
30 Correct 7 ms 5332 KB Output is correct
31 Correct 9 ms 5348 KB Output is correct
32 Correct 9 ms 5412 KB Output is correct
33 Correct 9 ms 5460 KB Output is correct
34 Correct 6 ms 5276 KB Output is correct
35 Correct 6 ms 5276 KB Output is correct
36 Correct 8 ms 5332 KB Output is correct
37 Correct 6 ms 5480 KB Output is correct
38 Correct 7 ms 5460 KB Output is correct
39 Correct 533 ms 20568 KB Output is correct
40 Correct 483 ms 20200 KB Output is correct
41 Correct 512 ms 20564 KB Output is correct
42 Correct 541 ms 20844 KB Output is correct
43 Correct 539 ms 20776 KB Output is correct
44 Correct 511 ms 20828 KB Output is correct
45 Correct 816 ms 25920 KB Output is correct
46 Correct 689 ms 29884 KB Output is correct
47 Correct 248 ms 21204 KB Output is correct
48 Correct 198 ms 21212 KB Output is correct
49 Correct 521 ms 21952 KB Output is correct
50 Correct 219 ms 21320 KB Output is correct
51 Correct 402 ms 29376 KB Output is correct