Submission #697204

# Submission time Handle Problem Language Result Execution time Memory
697204 2023-02-08T21:31:14 Z flappybird Capital City (JOI20_capital_city) C++17
100 / 100
604 ms 43588 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 201010
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 1000000007
vector<int> adj[MAX];
int col[MAX];
int prt[MAX];
int num[MAX];
int vis[MAX];
int C[MAX];
int used[MAX];
int cvis[MAX];
void make_num(int x, int c, int p = 0) {
	col[x] = c;
	num[x] = 1;
	for (auto v : adj[x]) {
		if (col[v] == c) continue;
		if (vis[v]) continue;
		make_num(v, c, x);
		num[x] += num[v];
	}
}
void dfs(int x, int c, int p = 0) {
	prt[x] = p;
	used[x] = 0;
	for (auto v : adj[x]) {
		if (col[v] != c) continue;
		if (v == p) continue;
		if (vis[v]) continue;
		dfs(v, c, x);
	}
}
int cnt, ans, N, K;
vector<int> cs[MAX];
void make_tree(int x) {
	cnt++;
	make_num(x, cnt);
	int c = x;
	int n = num[x];
	while (1) {
		int changed = 0;
		for (auto v : adj[c]) {
			if (vis[v]) continue;
			if (num[c] < num[v]) continue;
			if (num[v] > n / 2) {
				changed = 1;
				c = v;
				break;
			}
		}
		if (!changed) break;
	}
	vector<int> all;
	queue<int> q;
	int sum = 0;
	dfs(c, cnt);
	q.push(C[c]);
	int pos = 1;
	used[c] = 1;
	while (q.size()) {
		int c = q.front();
		q.pop();
		if (cvis[c]) continue;
		cvis[c] = 1;
		all.push_back(c);
		sum++;
		for (auto v : cs[c]) {
			if (col[v] != cnt) {
				pos = 0;
				break;
			}
			while (1) {
				if (used[v]) break;
				q.push(C[v]);
				used[v] = 1;
				v = prt[v];
			}
		}
		if (!pos) break;
	}
	for (auto v : all) cvis[v] = 0;
	if (pos) ans = min(ans, sum);
	vis[c] = 1;
	for (auto v : adj[c]) if (!vis[v]) make_tree(v);
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int N, K;
	cin >> N >> K;
	ans = N;
	int i, a, b;
	for (i = 1; i < N; i++) {
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	for (i = 1; i <= N; i++) cin >> C[i], cs[C[i]].push_back(i);
	make_tree(1);
	cout << ans - 1 << ln;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 5 ms 9772 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9776 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 5 ms 9772 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9776 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 7 ms 9940 KB Output is correct
12 Correct 6 ms 9968 KB Output is correct
13 Correct 9 ms 9960 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 7 ms 9940 KB Output is correct
16 Correct 8 ms 9940 KB Output is correct
17 Correct 8 ms 9972 KB Output is correct
18 Correct 7 ms 9940 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 6 ms 10048 KB Output is correct
21 Correct 6 ms 10044 KB Output is correct
22 Correct 8 ms 10044 KB Output is correct
23 Correct 8 ms 9984 KB Output is correct
24 Correct 7 ms 9908 KB Output is correct
25 Correct 7 ms 10068 KB Output is correct
26 Correct 7 ms 10068 KB Output is correct
27 Correct 7 ms 10068 KB Output is correct
28 Correct 7 ms 10044 KB Output is correct
29 Correct 7 ms 9940 KB Output is correct
30 Correct 7 ms 10044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 495 ms 43092 KB Output is correct
2 Correct 172 ms 43480 KB Output is correct
3 Correct 500 ms 42816 KB Output is correct
4 Correct 166 ms 43500 KB Output is correct
5 Correct 483 ms 39904 KB Output is correct
6 Correct 168 ms 43552 KB Output is correct
7 Correct 479 ms 40508 KB Output is correct
8 Correct 164 ms 43532 KB Output is correct
9 Correct 578 ms 38852 KB Output is correct
10 Correct 576 ms 36288 KB Output is correct
11 Correct 583 ms 39312 KB Output is correct
12 Correct 583 ms 42052 KB Output is correct
13 Correct 556 ms 35788 KB Output is correct
14 Correct 537 ms 42264 KB Output is correct
15 Correct 562 ms 41924 KB Output is correct
16 Correct 551 ms 36880 KB Output is correct
17 Correct 545 ms 37296 KB Output is correct
18 Correct 552 ms 37704 KB Output is correct
19 Correct 570 ms 40748 KB Output is correct
20 Correct 548 ms 43024 KB Output is correct
21 Correct 6 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 5 ms 9772 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9776 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 7 ms 9940 KB Output is correct
12 Correct 6 ms 9968 KB Output is correct
13 Correct 9 ms 9960 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 7 ms 9940 KB Output is correct
16 Correct 8 ms 9940 KB Output is correct
17 Correct 8 ms 9972 KB Output is correct
18 Correct 7 ms 9940 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 6 ms 10048 KB Output is correct
21 Correct 6 ms 10044 KB Output is correct
22 Correct 8 ms 10044 KB Output is correct
23 Correct 8 ms 9984 KB Output is correct
24 Correct 7 ms 9908 KB Output is correct
25 Correct 7 ms 10068 KB Output is correct
26 Correct 7 ms 10068 KB Output is correct
27 Correct 7 ms 10068 KB Output is correct
28 Correct 7 ms 10044 KB Output is correct
29 Correct 7 ms 9940 KB Output is correct
30 Correct 7 ms 10044 KB Output is correct
31 Correct 495 ms 43092 KB Output is correct
32 Correct 172 ms 43480 KB Output is correct
33 Correct 500 ms 42816 KB Output is correct
34 Correct 166 ms 43500 KB Output is correct
35 Correct 483 ms 39904 KB Output is correct
36 Correct 168 ms 43552 KB Output is correct
37 Correct 479 ms 40508 KB Output is correct
38 Correct 164 ms 43532 KB Output is correct
39 Correct 578 ms 38852 KB Output is correct
40 Correct 576 ms 36288 KB Output is correct
41 Correct 583 ms 39312 KB Output is correct
42 Correct 583 ms 42052 KB Output is correct
43 Correct 556 ms 35788 KB Output is correct
44 Correct 537 ms 42264 KB Output is correct
45 Correct 562 ms 41924 KB Output is correct
46 Correct 551 ms 36880 KB Output is correct
47 Correct 545 ms 37296 KB Output is correct
48 Correct 552 ms 37704 KB Output is correct
49 Correct 570 ms 40748 KB Output is correct
50 Correct 548 ms 43024 KB Output is correct
51 Correct 6 ms 9684 KB Output is correct
52 Correct 392 ms 26640 KB Output is correct
53 Correct 385 ms 26696 KB Output is correct
54 Correct 409 ms 26732 KB Output is correct
55 Correct 368 ms 26740 KB Output is correct
56 Correct 404 ms 26696 KB Output is correct
57 Correct 380 ms 26744 KB Output is correct
58 Correct 373 ms 30268 KB Output is correct
59 Correct 325 ms 30368 KB Output is correct
60 Correct 443 ms 29684 KB Output is correct
61 Correct 501 ms 29428 KB Output is correct
62 Correct 166 ms 43572 KB Output is correct
63 Correct 170 ms 43496 KB Output is correct
64 Correct 209 ms 43440 KB Output is correct
65 Correct 181 ms 43588 KB Output is correct
66 Correct 298 ms 35220 KB Output is correct
67 Correct 311 ms 35196 KB Output is correct
68 Correct 328 ms 35340 KB Output is correct
69 Correct 336 ms 35292 KB Output is correct
70 Correct 353 ms 35044 KB Output is correct
71 Correct 310 ms 35140 KB Output is correct
72 Correct 336 ms 35188 KB Output is correct
73 Correct 331 ms 34488 KB Output is correct
74 Correct 321 ms 35272 KB Output is correct
75 Correct 356 ms 35248 KB Output is correct
76 Correct 456 ms 33884 KB Output is correct
77 Correct 489 ms 32188 KB Output is correct
78 Correct 575 ms 37516 KB Output is correct
79 Correct 585 ms 35452 KB Output is correct
80 Correct 603 ms 42396 KB Output is correct
81 Correct 598 ms 38996 KB Output is correct
82 Correct 576 ms 39108 KB Output is correct
83 Correct 604 ms 35736 KB Output is correct
84 Correct 571 ms 41640 KB Output is correct
85 Correct 573 ms 40128 KB Output is correct
86 Correct 563 ms 35288 KB Output is correct
87 Correct 586 ms 37080 KB Output is correct
88 Correct 500 ms 38020 KB Output is correct
89 Correct 490 ms 34128 KB Output is correct
90 Correct 479 ms 33816 KB Output is correct
91 Correct 506 ms 36160 KB Output is correct
92 Correct 496 ms 34848 KB Output is correct
93 Correct 487 ms 34508 KB Output is correct
94 Correct 460 ms 33832 KB Output is correct
95 Correct 515 ms 35368 KB Output is correct
96 Correct 486 ms 34128 KB Output is correct
97 Correct 483 ms 36140 KB Output is correct