Submission #220623

# Submission time Handle Problem Language Result Execution time Memory
220623 2020-04-08T09:45:23 Z atoiz Mergers (JOI19_mergers) C++14
100 / 100
786 ms 94388 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 500007;
int N, K, ans = 0;
int dsu[MAXN], group[MAXN], last[MAXN], val[MAXN], deg[MAXN];
vector<int> adj[MAXN], add[MAXN];

int root(int u) { return (dsu[u] != u ? dsu[u] = root(dsu[u]) : u); }

void dfs(int u, int p)
{
	dsu[u] = u;
	for (int v : add[u]) {
		if (dsu[v]) val[root(v)] -= 2;
	}

	for (int v : adj[u]) if (v != p) {
		dfs(v, u);
		dsu[v] = u;
		val[u] += val[v];
		deg[u] += (val[v] ? deg[v] : 1);
	}

	if (p == 0) ans += (deg[u] == 1);
	else if (!val[u]) ans += (deg[u] + !val[u] == 1);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> K;
	for (int i = 0; i < N - 1; ++i) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for (int u = 1; u <= N; ++u) {
		cin >> group[u];
		if (last[group[u]]) {
			int v = last[group[u]];
			add[u].push_back(v);
			add[v].push_back(u);
			++val[u], ++val[v];
		}
		last[group[u]] = u;
	}

	if (N == 2) return cout << K - 1 << endl, 0;

	dfs(1, 0);
	// cout << ans << endl;
	cout << (ans + 1) / 2 << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 18 ms 23936 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 19 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 18 ms 23936 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23936 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23936 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 20 ms 23936 KB Output is correct
23 Correct 17 ms 23968 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 18 ms 23936 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 19 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 18 ms 23936 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23936 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23936 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 20 ms 23936 KB Output is correct
23 Correct 17 ms 23968 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 24064 KB Output is correct
27 Correct 20 ms 24192 KB Output is correct
28 Correct 18 ms 24064 KB Output is correct
29 Correct 20 ms 24064 KB Output is correct
30 Correct 21 ms 24064 KB Output is correct
31 Correct 17 ms 23808 KB Output is correct
32 Correct 21 ms 24192 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 19 ms 24064 KB Output is correct
35 Correct 20 ms 24064 KB Output is correct
36 Correct 19 ms 24064 KB Output is correct
37 Correct 23 ms 24184 KB Output is correct
38 Correct 17 ms 23936 KB Output is correct
39 Correct 20 ms 24192 KB Output is correct
40 Correct 19 ms 24192 KB Output is correct
41 Correct 19 ms 24192 KB Output is correct
42 Correct 19 ms 24192 KB Output is correct
43 Correct 19 ms 24320 KB Output is correct
44 Correct 17 ms 23808 KB Output is correct
45 Correct 21 ms 24064 KB Output is correct
46 Correct 20 ms 24192 KB Output is correct
47 Correct 19 ms 23808 KB Output is correct
48 Correct 23 ms 24192 KB Output is correct
49 Correct 19 ms 24064 KB Output is correct
50 Correct 20 ms 24064 KB Output is correct
51 Correct 19 ms 24192 KB Output is correct
52 Correct 20 ms 24192 KB Output is correct
53 Correct 21 ms 24192 KB Output is correct
54 Correct 19 ms 24192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 18 ms 23936 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 19 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 18 ms 23936 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23936 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23936 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 20 ms 23936 KB Output is correct
23 Correct 17 ms 23968 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 18 ms 23936 KB Output is correct
26 Correct 90 ms 31728 KB Output is correct
27 Correct 109 ms 31740 KB Output is correct
28 Correct 20 ms 24064 KB Output is correct
29 Correct 18 ms 23808 KB Output is correct
30 Correct 18 ms 23808 KB Output is correct
31 Correct 102 ms 31864 KB Output is correct
32 Correct 19 ms 24064 KB Output is correct
33 Correct 118 ms 37496 KB Output is correct
34 Correct 115 ms 31868 KB Output is correct
35 Correct 19 ms 24064 KB Output is correct
36 Correct 126 ms 33800 KB Output is correct
37 Correct 19 ms 24192 KB Output is correct
38 Correct 19 ms 24192 KB Output is correct
39 Correct 95 ms 33136 KB Output is correct
40 Correct 20 ms 24320 KB Output is correct
41 Correct 107 ms 33144 KB Output is correct
42 Correct 107 ms 35064 KB Output is correct
43 Correct 17 ms 23808 KB Output is correct
44 Correct 117 ms 39032 KB Output is correct
45 Correct 114 ms 36216 KB Output is correct
46 Correct 19 ms 24192 KB Output is correct
47 Correct 21 ms 24192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 31744 KB Output is correct
2 Correct 77 ms 28508 KB Output is correct
3 Correct 19 ms 24064 KB Output is correct
4 Correct 19 ms 24192 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 19 ms 24192 KB Output is correct
8 Correct 106 ms 31352 KB Output is correct
9 Correct 20 ms 24192 KB Output is correct
10 Correct 101 ms 33272 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 116 ms 33272 KB Output is correct
13 Correct 129 ms 33144 KB Output is correct
14 Correct 86 ms 30840 KB Output is correct
15 Correct 95 ms 33136 KB Output is correct
16 Correct 19 ms 24192 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 76 ms 31344 KB Output is correct
19 Correct 94 ms 33912 KB Output is correct
20 Correct 19 ms 24112 KB Output is correct
21 Correct 22 ms 23936 KB Output is correct
22 Correct 82 ms 33008 KB Output is correct
23 Correct 19 ms 24192 KB Output is correct
24 Correct 105 ms 33400 KB Output is correct
25 Correct 86 ms 32760 KB Output is correct
26 Correct 18 ms 24064 KB Output is correct
27 Correct 19 ms 24192 KB Output is correct
28 Correct 18 ms 24192 KB Output is correct
29 Correct 19 ms 24192 KB Output is correct
30 Correct 19 ms 24192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 18 ms 23936 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 19 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 18 ms 23936 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23936 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23936 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 20 ms 23936 KB Output is correct
23 Correct 17 ms 23968 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 24064 KB Output is correct
27 Correct 20 ms 24192 KB Output is correct
28 Correct 18 ms 24064 KB Output is correct
29 Correct 20 ms 24064 KB Output is correct
30 Correct 21 ms 24064 KB Output is correct
31 Correct 17 ms 23808 KB Output is correct
32 Correct 21 ms 24192 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 19 ms 24064 KB Output is correct
35 Correct 20 ms 24064 KB Output is correct
36 Correct 19 ms 24064 KB Output is correct
37 Correct 23 ms 24184 KB Output is correct
38 Correct 17 ms 23936 KB Output is correct
39 Correct 20 ms 24192 KB Output is correct
40 Correct 19 ms 24192 KB Output is correct
41 Correct 19 ms 24192 KB Output is correct
42 Correct 19 ms 24192 KB Output is correct
43 Correct 19 ms 24320 KB Output is correct
44 Correct 17 ms 23808 KB Output is correct
45 Correct 21 ms 24064 KB Output is correct
46 Correct 20 ms 24192 KB Output is correct
47 Correct 19 ms 23808 KB Output is correct
48 Correct 23 ms 24192 KB Output is correct
49 Correct 19 ms 24064 KB Output is correct
50 Correct 20 ms 24064 KB Output is correct
51 Correct 19 ms 24192 KB Output is correct
52 Correct 20 ms 24192 KB Output is correct
53 Correct 21 ms 24192 KB Output is correct
54 Correct 19 ms 24192 KB Output is correct
55 Correct 18 ms 23936 KB Output is correct
56 Correct 90 ms 31728 KB Output is correct
57 Correct 109 ms 31740 KB Output is correct
58 Correct 20 ms 24064 KB Output is correct
59 Correct 18 ms 23808 KB Output is correct
60 Correct 18 ms 23808 KB Output is correct
61 Correct 102 ms 31864 KB Output is correct
62 Correct 19 ms 24064 KB Output is correct
63 Correct 118 ms 37496 KB Output is correct
64 Correct 115 ms 31868 KB Output is correct
65 Correct 19 ms 24064 KB Output is correct
66 Correct 126 ms 33800 KB Output is correct
67 Correct 19 ms 24192 KB Output is correct
68 Correct 19 ms 24192 KB Output is correct
69 Correct 95 ms 33136 KB Output is correct
70 Correct 20 ms 24320 KB Output is correct
71 Correct 107 ms 33144 KB Output is correct
72 Correct 107 ms 35064 KB Output is correct
73 Correct 17 ms 23808 KB Output is correct
74 Correct 117 ms 39032 KB Output is correct
75 Correct 114 ms 36216 KB Output is correct
76 Correct 19 ms 24192 KB Output is correct
77 Correct 21 ms 24192 KB Output is correct
78 Correct 84 ms 31744 KB Output is correct
79 Correct 77 ms 28508 KB Output is correct
80 Correct 19 ms 24064 KB Output is correct
81 Correct 19 ms 24192 KB Output is correct
82 Correct 18 ms 23808 KB Output is correct
83 Correct 18 ms 23808 KB Output is correct
84 Correct 19 ms 24192 KB Output is correct
85 Correct 106 ms 31352 KB Output is correct
86 Correct 20 ms 24192 KB Output is correct
87 Correct 101 ms 33272 KB Output is correct
88 Correct 18 ms 23808 KB Output is correct
89 Correct 116 ms 33272 KB Output is correct
90 Correct 129 ms 33144 KB Output is correct
91 Correct 86 ms 30840 KB Output is correct
92 Correct 95 ms 33136 KB Output is correct
93 Correct 19 ms 24192 KB Output is correct
94 Correct 18 ms 23808 KB Output is correct
95 Correct 76 ms 31344 KB Output is correct
96 Correct 94 ms 33912 KB Output is correct
97 Correct 19 ms 24112 KB Output is correct
98 Correct 22 ms 23936 KB Output is correct
99 Correct 82 ms 33008 KB Output is correct
100 Correct 19 ms 24192 KB Output is correct
101 Correct 105 ms 33400 KB Output is correct
102 Correct 86 ms 32760 KB Output is correct
103 Correct 18 ms 24064 KB Output is correct
104 Correct 19 ms 24192 KB Output is correct
105 Correct 18 ms 24192 KB Output is correct
106 Correct 19 ms 24192 KB Output is correct
107 Correct 19 ms 24192 KB Output is correct
108 Correct 592 ms 72384 KB Output is correct
109 Correct 688 ms 85368 KB Output is correct
110 Correct 699 ms 94388 KB Output is correct
111 Correct 629 ms 78328 KB Output is correct
112 Correct 593 ms 70264 KB Output is correct
113 Correct 402 ms 62176 KB Output is correct
114 Correct 648 ms 71160 KB Output is correct
115 Correct 636 ms 71416 KB Output is correct
116 Correct 786 ms 73464 KB Output is correct
117 Correct 564 ms 59768 KB Output is correct
118 Correct 683 ms 71096 KB Output is correct
119 Correct 540 ms 59768 KB Output is correct
120 Correct 588 ms 70776 KB Output is correct
121 Correct 574 ms 59848 KB Output is correct
122 Correct 677 ms 69916 KB Output is correct
123 Correct 348 ms 57316 KB Output is correct
124 Correct 583 ms 58876 KB Output is correct