Submission #419475

# Submission time Handle Problem Language Result Execution time Memory
419475 2021-06-07T07:37:10 Z amoo_safar Meetings 2 (JOI21_meetings2) C++17
100 / 100
536 ms 49220 KB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second

using namespace std;

const int N = 2e5 + 10;
const int Log = 20;

vector<int> G[N];
vector< pair<int, int> > E[N];

int n;
int sp[N][Log];
int sub[N], dep[N];

int DFS(int u, int p, int d){
	sp[u][0] = p;
	sub[u] = 1;
	dep[u] = d;
	for(int l = 1; l < Log; l++)
		sp[u][l] = sp[ sp[u][l - 1] ][l - 1];
	for(auto adj : G[u])
		if(adj != p)
			sub[u] += DFS(adj, u, d + 1);
	E[min(n - sub[u], sub[u])].pb({u, p});
	return sub[u];
}

int LCA(int u, int v){
	if(dep[u] < dep[v]) swap(u, v);
	for(int i = 0; i < Log; i++)
		if((dep[u] - dep[v]) >> i & 1)
			u = sp[u][i];
	if(u == v) return u;
	for(int l = Log - 1; l >= 0; l--)
		if(sp[u][l] != sp[v][l])
			u = sp[u][l], v = sp[v][l];
	return sp[u][0];
}
int dist(int u, int v){ return dep[u] + dep[v] - 2 * dep[LCA(u, v)]; }

int ans[N];
int mk[N];

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	int u, v;
	for(int i = 1; i < n; i++){
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
	}
	DFS(1, 0, 0);
	
	u = -1, v = -1;
	for(int i = n; i >= 1; i--){
		if(i & 1){
			ans[i] = 1;
			continue;
		}
		for(auto [fr, to] : E[i / 2]){
			if(u == -1) u = fr, v = to, mk[fr] = 1, mk[to] = 1;

			if(!mk[fr]){
				if(dist(u, v) < dist(u, fr))
					v = fr;
				if(dist(u, v) < dist(v, fr))
					u = fr;
				mk[fr] = 1;
			}
			if(!mk[to]){
				if(dist(u, v) < dist(u, to))
					v = to;
				if(dist(u, v) < dist(v, to))
					u = to;
				mk[to] = 1;
			}
		}
		ans[i] = u == -1 ? 1 : dist(u, v) + 1;
	}
	for(int i = 1; i <= n; i++)
		cout << ans[i] << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9648 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 7 ms 9620 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
12 Correct 8 ms 9676 KB Output is correct
13 Correct 8 ms 9676 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9696 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 7 ms 9676 KB Output is correct
20 Correct 7 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9648 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 7 ms 9620 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
12 Correct 8 ms 9676 KB Output is correct
13 Correct 8 ms 9676 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9696 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 7 ms 9676 KB Output is correct
20 Correct 7 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
22 Correct 12 ms 10284 KB Output is correct
23 Correct 10 ms 10188 KB Output is correct
24 Correct 10 ms 10188 KB Output is correct
25 Correct 11 ms 10316 KB Output is correct
26 Correct 12 ms 10284 KB Output is correct
27 Correct 10 ms 10304 KB Output is correct
28 Correct 12 ms 10188 KB Output is correct
29 Correct 12 ms 10188 KB Output is correct
30 Correct 11 ms 10188 KB Output is correct
31 Correct 11 ms 10316 KB Output is correct
32 Correct 11 ms 10444 KB Output is correct
33 Correct 12 ms 10444 KB Output is correct
34 Correct 11 ms 10188 KB Output is correct
35 Correct 12 ms 10320 KB Output is correct
36 Correct 10 ms 10316 KB Output is correct
37 Correct 10 ms 10316 KB Output is correct
38 Correct 11 ms 10316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9648 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 7 ms 9620 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
12 Correct 8 ms 9676 KB Output is correct
13 Correct 8 ms 9676 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9696 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 7 ms 9676 KB Output is correct
20 Correct 7 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
22 Correct 12 ms 10284 KB Output is correct
23 Correct 10 ms 10188 KB Output is correct
24 Correct 10 ms 10188 KB Output is correct
25 Correct 11 ms 10316 KB Output is correct
26 Correct 12 ms 10284 KB Output is correct
27 Correct 10 ms 10304 KB Output is correct
28 Correct 12 ms 10188 KB Output is correct
29 Correct 12 ms 10188 KB Output is correct
30 Correct 11 ms 10188 KB Output is correct
31 Correct 11 ms 10316 KB Output is correct
32 Correct 11 ms 10444 KB Output is correct
33 Correct 12 ms 10444 KB Output is correct
34 Correct 11 ms 10188 KB Output is correct
35 Correct 12 ms 10320 KB Output is correct
36 Correct 10 ms 10316 KB Output is correct
37 Correct 10 ms 10316 KB Output is correct
38 Correct 11 ms 10316 KB Output is correct
39 Correct 361 ms 36968 KB Output is correct
40 Correct 343 ms 35996 KB Output is correct
41 Correct 354 ms 37060 KB Output is correct
42 Correct 360 ms 37336 KB Output is correct
43 Correct 342 ms 37448 KB Output is correct
44 Correct 329 ms 37436 KB Output is correct
45 Correct 536 ms 44288 KB Output is correct
46 Correct 466 ms 49220 KB Output is correct
47 Correct 312 ms 37592 KB Output is correct
48 Correct 282 ms 37624 KB Output is correct
49 Correct 380 ms 37924 KB Output is correct
50 Correct 315 ms 37556 KB Output is correct
51 Correct 360 ms 47204 KB Output is correct