Submission #376791

# Submission time Handle Problem Language Result Execution time Memory
376791 2021-03-12T04:04:22 Z 8e7 Mergers (JOI19_mergers) C++14
48 / 100
3000 ms 36204 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#define ll long long
#define maxn 500005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
vector<int> adj[maxn], tree[maxn];
int col[maxn], cnt[maxn], siz[maxn], par[maxn], ind[maxn];
bool poss[maxn];

int cur;
void dfs(int n, int pa) {
	siz[n] = col[n] == cur ? 1 : 0;
	par[n] = pa;
	for (int v:adj[n]) {
		if (v != pa) {
			dfs(v, n);
			siz[n] += siz[v];
		}
	}
	if (siz[n] > 0 && siz[n] < cnt[cur]) poss[n] = true;
}

int find(int a) {
	return a == ind[a] ? a : ind[a] = find(ind[a]);
}
void Union(int a, int b) {
	ind[find(a)] = find(b);
}

int ans = 0;
void dfs2(int n, int par) {
	int chi = 0;
	for (int v:tree[n]) {
		if (v != par) {
			dfs2(v, n);
			chi++;
		}
	}
	if (chi == 0 || (n == 1 && chi == 1)) ans++;
}
int main() {
	io
	int n, k;
	cin >> n >> k;
	for (int i = 0;i < n - 1;i++) {
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	for (int i = 1;i <= n;i++) {
		cin >> col[i];
		cnt[col[i]]++;
		ind[i] = i;
	}
	for (int i = 1;i <= k;i++) {
		for (int j = 1;j <= n;j++) siz[j] = 0;
		cur = i;
		dfs(1, 0);
	}
	for (int i = 1;i <= n;i++) {
		if (poss[i]) Union(i, par[i]);
	}
	int done = 1;
	for (int i = 1;i <= n;i++) {
		if (par[i] != 0 && !poss[i]) {
			done = 0;
			int a = find(i), b = find(par[i]);
			tree[a].push_back(b);
			tree[b].push_back(a);
		}
	}
	if (done) cout << 0 << "\n";
	else {
		dfs2(find(1), 0);
		cout << (ans + 1) / 2 << "\n";
	}
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4

5 4
1 2
2 3
3 4
4 5
1
2
3
4
1

2 2
1 2
1
2

 */
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 16 ms 23916 KB Output is correct
9 Correct 16 ms 23916 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 16 ms 23928 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 16 ms 23916 KB Output is correct
20 Correct 18 ms 23916 KB Output is correct
21 Correct 16 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 17 ms 23916 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 16 ms 23916 KB Output is correct
9 Correct 16 ms 23916 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 16 ms 23928 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 16 ms 23916 KB Output is correct
20 Correct 18 ms 23916 KB Output is correct
21 Correct 16 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 17 ms 23916 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
25 Correct 16 ms 23916 KB Output is correct
26 Correct 146 ms 24172 KB Output is correct
27 Correct 22 ms 24044 KB Output is correct
28 Correct 193 ms 24192 KB Output is correct
29 Correct 195 ms 24168 KB Output is correct
30 Correct 30 ms 24192 KB Output is correct
31 Correct 16 ms 23916 KB Output is correct
32 Correct 262 ms 24300 KB Output is correct
33 Correct 18 ms 23916 KB Output is correct
34 Correct 20 ms 24044 KB Output is correct
35 Correct 192 ms 24092 KB Output is correct
36 Correct 21 ms 24044 KB Output is correct
37 Correct 107 ms 24044 KB Output is correct
38 Correct 17 ms 23916 KB Output is correct
39 Correct 108 ms 24172 KB Output is correct
40 Correct 19 ms 24044 KB Output is correct
41 Correct 18 ms 24044 KB Output is correct
42 Correct 62 ms 24044 KB Output is correct
43 Correct 18 ms 24300 KB Output is correct
44 Correct 17 ms 23936 KB Output is correct
45 Correct 243 ms 24044 KB Output is correct
46 Correct 125 ms 24044 KB Output is correct
47 Correct 16 ms 23916 KB Output is correct
48 Correct 56 ms 24064 KB Output is correct
49 Correct 106 ms 24172 KB Output is correct
50 Correct 198 ms 24172 KB Output is correct
51 Correct 58 ms 24044 KB Output is correct
52 Correct 19 ms 24044 KB Output is correct
53 Correct 60 ms 24044 KB Output is correct
54 Correct 21 ms 24044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 16 ms 23916 KB Output is correct
9 Correct 16 ms 23916 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 16 ms 23928 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 16 ms 23916 KB Output is correct
20 Correct 18 ms 23916 KB Output is correct
21 Correct 16 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 17 ms 23916 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
25 Correct 16 ms 23916 KB Output is correct
26 Correct 189 ms 29028 KB Output is correct
27 Correct 626 ms 28928 KB Output is correct
28 Correct 22 ms 24044 KB Output is correct
29 Correct 17 ms 23916 KB Output is correct
30 Correct 16 ms 23916 KB Output is correct
31 Correct 641 ms 28780 KB Output is correct
32 Correct 19 ms 24044 KB Output is correct
33 Correct 1039 ms 36076 KB Output is correct
34 Correct 610 ms 28780 KB Output is correct
35 Correct 21 ms 24172 KB Output is correct
36 Correct 911 ms 29676 KB Output is correct
37 Correct 18 ms 24044 KB Output is correct
38 Correct 22 ms 24044 KB Output is correct
39 Correct 216 ms 29284 KB Output is correct
40 Correct 19 ms 24300 KB Output is correct
41 Correct 101 ms 28780 KB Output is correct
42 Correct 992 ms 31184 KB Output is correct
43 Correct 17 ms 23916 KB Output is correct
44 Correct 100 ms 36204 KB Output is correct
45 Correct 770 ms 32620 KB Output is correct
46 Correct 19 ms 24044 KB Output is correct
47 Correct 23 ms 24044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 29060 KB Output is correct
2 Execution timed out 3093 ms 29284 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 16 ms 23916 KB Output is correct
9 Correct 16 ms 23916 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23916 KB Output is correct
16 Correct 16 ms 23916 KB Output is correct
17 Correct 16 ms 23928 KB Output is correct
18 Correct 17 ms 23916 KB Output is correct
19 Correct 16 ms 23916 KB Output is correct
20 Correct 18 ms 23916 KB Output is correct
21 Correct 16 ms 23916 KB Output is correct
22 Correct 17 ms 23916 KB Output is correct
23 Correct 17 ms 23916 KB Output is correct
24 Correct 17 ms 23916 KB Output is correct
25 Correct 16 ms 23916 KB Output is correct
26 Correct 146 ms 24172 KB Output is correct
27 Correct 22 ms 24044 KB Output is correct
28 Correct 193 ms 24192 KB Output is correct
29 Correct 195 ms 24168 KB Output is correct
30 Correct 30 ms 24192 KB Output is correct
31 Correct 16 ms 23916 KB Output is correct
32 Correct 262 ms 24300 KB Output is correct
33 Correct 18 ms 23916 KB Output is correct
34 Correct 20 ms 24044 KB Output is correct
35 Correct 192 ms 24092 KB Output is correct
36 Correct 21 ms 24044 KB Output is correct
37 Correct 107 ms 24044 KB Output is correct
38 Correct 17 ms 23916 KB Output is correct
39 Correct 108 ms 24172 KB Output is correct
40 Correct 19 ms 24044 KB Output is correct
41 Correct 18 ms 24044 KB Output is correct
42 Correct 62 ms 24044 KB Output is correct
43 Correct 18 ms 24300 KB Output is correct
44 Correct 17 ms 23936 KB Output is correct
45 Correct 243 ms 24044 KB Output is correct
46 Correct 125 ms 24044 KB Output is correct
47 Correct 16 ms 23916 KB Output is correct
48 Correct 56 ms 24064 KB Output is correct
49 Correct 106 ms 24172 KB Output is correct
50 Correct 198 ms 24172 KB Output is correct
51 Correct 58 ms 24044 KB Output is correct
52 Correct 19 ms 24044 KB Output is correct
53 Correct 60 ms 24044 KB Output is correct
54 Correct 21 ms 24044 KB Output is correct
55 Correct 16 ms 23916 KB Output is correct
56 Correct 189 ms 29028 KB Output is correct
57 Correct 626 ms 28928 KB Output is correct
58 Correct 22 ms 24044 KB Output is correct
59 Correct 17 ms 23916 KB Output is correct
60 Correct 16 ms 23916 KB Output is correct
61 Correct 641 ms 28780 KB Output is correct
62 Correct 19 ms 24044 KB Output is correct
63 Correct 1039 ms 36076 KB Output is correct
64 Correct 610 ms 28780 KB Output is correct
65 Correct 21 ms 24172 KB Output is correct
66 Correct 911 ms 29676 KB Output is correct
67 Correct 18 ms 24044 KB Output is correct
68 Correct 22 ms 24044 KB Output is correct
69 Correct 216 ms 29284 KB Output is correct
70 Correct 19 ms 24300 KB Output is correct
71 Correct 101 ms 28780 KB Output is correct
72 Correct 992 ms 31184 KB Output is correct
73 Correct 17 ms 23916 KB Output is correct
74 Correct 100 ms 36204 KB Output is correct
75 Correct 770 ms 32620 KB Output is correct
76 Correct 19 ms 24044 KB Output is correct
77 Correct 23 ms 24044 KB Output is correct
78 Correct 161 ms 29060 KB Output is correct
79 Execution timed out 3093 ms 29284 KB Time limit exceeded
80 Halted 0 ms 0 KB -