Submission #543272

# Submission time Handle Problem Language Result Execution time Memory
543272 2022-03-30T02:50:24 Z LittleCube Meetings 2 (JOI21_meetings2) C++14
100 / 100
674 ms 27452 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()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	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 2 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 2 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 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 3 ms 4980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 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 2 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 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 3 ms 4980 KB Output is correct
22 Correct 6 ms 5332 KB Output is correct
23 Correct 6 ms 5332 KB Output is correct
24 Correct 5 ms 5332 KB Output is correct
25 Correct 6 ms 5332 KB Output is correct
26 Correct 6 ms 5332 KB Output is correct
27 Correct 6 ms 5332 KB Output is correct
28 Correct 7 ms 5332 KB Output is correct
29 Correct 6 ms 5332 KB Output is correct
30 Correct 7 ms 5332 KB Output is correct
31 Correct 6 ms 5332 KB Output is correct
32 Correct 7 ms 5460 KB Output is correct
33 Correct 8 ms 5460 KB Output is correct
34 Correct 4 ms 5332 KB Output is correct
35 Correct 4 ms 5332 KB Output is correct
36 Correct 5 ms 5332 KB Output is correct
37 Correct 5 ms 5332 KB Output is correct
38 Correct 5 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 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 2 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 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 3 ms 4980 KB Output is correct
22 Correct 6 ms 5332 KB Output is correct
23 Correct 6 ms 5332 KB Output is correct
24 Correct 5 ms 5332 KB Output is correct
25 Correct 6 ms 5332 KB Output is correct
26 Correct 6 ms 5332 KB Output is correct
27 Correct 6 ms 5332 KB Output is correct
28 Correct 7 ms 5332 KB Output is correct
29 Correct 6 ms 5332 KB Output is correct
30 Correct 7 ms 5332 KB Output is correct
31 Correct 6 ms 5332 KB Output is correct
32 Correct 7 ms 5460 KB Output is correct
33 Correct 8 ms 5460 KB Output is correct
34 Correct 4 ms 5332 KB Output is correct
35 Correct 4 ms 5332 KB Output is correct
36 Correct 5 ms 5332 KB Output is correct
37 Correct 5 ms 5332 KB Output is correct
38 Correct 5 ms 5332 KB Output is correct
39 Correct 435 ms 18096 KB Output is correct
40 Correct 392 ms 17748 KB Output is correct
41 Correct 404 ms 18120 KB Output is correct
42 Correct 382 ms 18268 KB Output is correct
43 Correct 413 ms 18332 KB Output is correct
44 Correct 390 ms 18328 KB Output is correct
45 Correct 674 ms 23440 KB Output is correct
46 Correct 530 ms 27452 KB Output is correct
47 Correct 168 ms 18480 KB Output is correct
48 Correct 113 ms 18824 KB Output is correct
49 Correct 369 ms 19448 KB Output is correct
50 Correct 140 ms 18888 KB Output is correct
51 Correct 313 ms 26912 KB Output is correct