Submission #543278

# Submission time Handle Problem Language Result Execution time Memory
543278 2022-03-30T03:06:51 Z LittleCube Meetings 2 (JOI21_meetings2) C++14
100 / 100
690 ms 27488 KB
#pragma GCC optimize("Ofast,unroll-loops")
#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 3 ms 4948 KB Output is correct
2 Correct 4 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 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 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 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 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 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 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 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5012 KB Output is correct
22 Correct 6 ms 5332 KB Output is correct
23 Correct 7 ms 5332 KB Output is correct
24 Correct 7 ms 5332 KB Output is correct
25 Correct 7 ms 5348 KB Output is correct
26 Correct 7 ms 5332 KB Output is correct
27 Correct 6 ms 5348 KB Output is correct
28 Correct 9 ms 5340 KB Output is correct
29 Correct 10 ms 5348 KB Output is correct
30 Correct 6 ms 5332 KB Output is correct
31 Correct 7 ms 5332 KB Output is correct
32 Correct 8 ms 5460 KB Output is correct
33 Correct 8 ms 5476 KB Output is correct
34 Correct 5 ms 5232 KB Output is correct
35 Correct 4 ms 5360 KB Output is correct
36 Correct 6 ms 5332 KB Output is correct
37 Correct 5 ms 5332 KB Output is correct
38 Correct 6 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 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 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 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 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5012 KB Output is correct
22 Correct 6 ms 5332 KB Output is correct
23 Correct 7 ms 5332 KB Output is correct
24 Correct 7 ms 5332 KB Output is correct
25 Correct 7 ms 5348 KB Output is correct
26 Correct 7 ms 5332 KB Output is correct
27 Correct 6 ms 5348 KB Output is correct
28 Correct 9 ms 5340 KB Output is correct
29 Correct 10 ms 5348 KB Output is correct
30 Correct 6 ms 5332 KB Output is correct
31 Correct 7 ms 5332 KB Output is correct
32 Correct 8 ms 5460 KB Output is correct
33 Correct 8 ms 5476 KB Output is correct
34 Correct 5 ms 5232 KB Output is correct
35 Correct 4 ms 5360 KB Output is correct
36 Correct 6 ms 5332 KB Output is correct
37 Correct 5 ms 5332 KB Output is correct
38 Correct 6 ms 5332 KB Output is correct
39 Correct 484 ms 18132 KB Output is correct
40 Correct 420 ms 17776 KB Output is correct
41 Correct 439 ms 18128 KB Output is correct
42 Correct 406 ms 18376 KB Output is correct
43 Correct 420 ms 18248 KB Output is correct
44 Correct 445 ms 18304 KB Output is correct
45 Correct 690 ms 23496 KB Output is correct
46 Correct 550 ms 27488 KB Output is correct
47 Correct 174 ms 18500 KB Output is correct
48 Correct 120 ms 18984 KB Output is correct
49 Correct 384 ms 19396 KB Output is correct
50 Correct 165 ms 18816 KB Output is correct
51 Correct 331 ms 26976 KB Output is correct