#include <bits/stdc++.h>
using namespace std;
class DisjointSet {
private:
int n;
vector<int> p;
public:
DisjointSet(int n) : n(n) {
p.resize(n);
iota(begin(p), end(p), 0);
}
int Find(int x) {
return p[x] == x ? x : p[x] = Find(p[x]);
}
bool Union(int x, int y) {
x = Find(x);
y = Find(y);
if (x == y) return false;
p[y] = x;
return true;
}
bool IsRoot(int x) {
return p[x] == x;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N, K;
cin >> N >> K;
vector<vector<int>> adj(N + 1);
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
vector<vector<int>> colorList(K);
for (int i = 1; i <= N; i++) {
int c;
cin >> c;
colorList[--c].emplace_back(i);
}
vector<int> par(N + 1);
vector<int> depth(N + 1);
DisjointSet group(N + 1);
function<void(int)> Dfs = [&](int n) {
for (auto i : adj[n]) if (i != par[n]) {
depth[i] = depth[n] + 1;
par[i] = n;
Dfs(i);
}
};
auto Unite = [&](int a, int b) {
a = group.Find(a);
b = group.Find(b);
while (a != b) {
if (depth[a] > depth[b]) swap(a, b);
group.Union(par[b], b);
b = group.Find(b);
}
};
depth[1] = 1;
par[1] = 0;
Dfs(1);
for (int k = 0; k < K; k++) {
for (int i = 1; i < (int) colorList[k].size(); i++) {
Unite(colorList[k][0], colorList[k][i]);
}
}
vector<int> degree(N + 1, 0);
for (int i = 1; i <= N; i++) {
for (auto j : adj[i]) {
if (group.Find(i) != group.Find(j)) {
degree[group.Find(i)]++;
}
}
}
int ans = 0;
for (int i = 1; i <= N; i++) {
if (group.IsRoot(i) && degree[i] == 1) {
ans++;
}
}
cout << ((ans + 1) / 2) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
448 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
448 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
6 ms |
768 KB |
Output is correct |
27 |
Correct |
6 ms |
640 KB |
Output is correct |
28 |
Correct |
7 ms |
896 KB |
Output is correct |
29 |
Correct |
7 ms |
768 KB |
Output is correct |
30 |
Correct |
6 ms |
640 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
896 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
35 |
Correct |
6 ms |
768 KB |
Output is correct |
36 |
Correct |
6 ms |
640 KB |
Output is correct |
37 |
Correct |
6 ms |
640 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
6 ms |
640 KB |
Output is correct |
40 |
Correct |
6 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
640 KB |
Output is correct |
43 |
Correct |
6 ms |
768 KB |
Output is correct |
44 |
Correct |
4 ms |
384 KB |
Output is correct |
45 |
Correct |
6 ms |
768 KB |
Output is correct |
46 |
Correct |
8 ms |
640 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
6 ms |
640 KB |
Output is correct |
49 |
Correct |
6 ms |
768 KB |
Output is correct |
50 |
Correct |
7 ms |
768 KB |
Output is correct |
51 |
Correct |
6 ms |
640 KB |
Output is correct |
52 |
Correct |
6 ms |
640 KB |
Output is correct |
53 |
Correct |
6 ms |
640 KB |
Output is correct |
54 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
448 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
54 ms |
9712 KB |
Output is correct |
27 |
Correct |
74 ms |
9464 KB |
Output is correct |
28 |
Correct |
6 ms |
640 KB |
Output is correct |
29 |
Correct |
5 ms |
388 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
72 ms |
9336 KB |
Output is correct |
32 |
Correct |
6 ms |
640 KB |
Output is correct |
33 |
Correct |
83 ms |
15096 KB |
Output is correct |
34 |
Correct |
73 ms |
9464 KB |
Output is correct |
35 |
Correct |
6 ms |
640 KB |
Output is correct |
36 |
Correct |
93 ms |
10016 KB |
Output is correct |
37 |
Correct |
6 ms |
640 KB |
Output is correct |
38 |
Correct |
6 ms |
640 KB |
Output is correct |
39 |
Correct |
56 ms |
9712 KB |
Output is correct |
40 |
Correct |
6 ms |
768 KB |
Output is correct |
41 |
Correct |
70 ms |
9328 KB |
Output is correct |
42 |
Correct |
87 ms |
11276 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
92 ms |
15508 KB |
Output is correct |
45 |
Correct |
77 ms |
12412 KB |
Output is correct |
46 |
Correct |
6 ms |
640 KB |
Output is correct |
47 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
9200 KB |
Output is correct |
2 |
Correct |
75 ms |
14192 KB |
Output is correct |
3 |
Correct |
7 ms |
768 KB |
Output is correct |
4 |
Correct |
6 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
640 KB |
Output is correct |
8 |
Correct |
93 ms |
11640 KB |
Output is correct |
9 |
Correct |
6 ms |
640 KB |
Output is correct |
10 |
Correct |
77 ms |
9464 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
85 ms |
9464 KB |
Output is correct |
13 |
Correct |
91 ms |
11640 KB |
Output is correct |
14 |
Correct |
87 ms |
14332 KB |
Output is correct |
15 |
Correct |
53 ms |
9712 KB |
Output is correct |
16 |
Correct |
6 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
74 ms |
14192 KB |
Output is correct |
19 |
Correct |
98 ms |
17528 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
70 ms |
12144 KB |
Output is correct |
23 |
Correct |
6 ms |
640 KB |
Output is correct |
24 |
Correct |
100 ms |
10364 KB |
Output is correct |
25 |
Correct |
89 ms |
16760 KB |
Output is correct |
26 |
Correct |
6 ms |
768 KB |
Output is correct |
27 |
Correct |
7 ms |
768 KB |
Output is correct |
28 |
Correct |
6 ms |
640 KB |
Output is correct |
29 |
Correct |
6 ms |
640 KB |
Output is correct |
30 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
448 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
6 ms |
768 KB |
Output is correct |
27 |
Correct |
6 ms |
640 KB |
Output is correct |
28 |
Correct |
7 ms |
896 KB |
Output is correct |
29 |
Correct |
7 ms |
768 KB |
Output is correct |
30 |
Correct |
6 ms |
640 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
896 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
35 |
Correct |
6 ms |
768 KB |
Output is correct |
36 |
Correct |
6 ms |
640 KB |
Output is correct |
37 |
Correct |
6 ms |
640 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
6 ms |
640 KB |
Output is correct |
40 |
Correct |
6 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
640 KB |
Output is correct |
43 |
Correct |
6 ms |
768 KB |
Output is correct |
44 |
Correct |
4 ms |
384 KB |
Output is correct |
45 |
Correct |
6 ms |
768 KB |
Output is correct |
46 |
Correct |
8 ms |
640 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
6 ms |
640 KB |
Output is correct |
49 |
Correct |
6 ms |
768 KB |
Output is correct |
50 |
Correct |
7 ms |
768 KB |
Output is correct |
51 |
Correct |
6 ms |
640 KB |
Output is correct |
52 |
Correct |
6 ms |
640 KB |
Output is correct |
53 |
Correct |
6 ms |
640 KB |
Output is correct |
54 |
Correct |
6 ms |
640 KB |
Output is correct |
55 |
Correct |
4 ms |
384 KB |
Output is correct |
56 |
Correct |
54 ms |
9712 KB |
Output is correct |
57 |
Correct |
74 ms |
9464 KB |
Output is correct |
58 |
Correct |
6 ms |
640 KB |
Output is correct |
59 |
Correct |
5 ms |
388 KB |
Output is correct |
60 |
Correct |
5 ms |
384 KB |
Output is correct |
61 |
Correct |
72 ms |
9336 KB |
Output is correct |
62 |
Correct |
6 ms |
640 KB |
Output is correct |
63 |
Correct |
83 ms |
15096 KB |
Output is correct |
64 |
Correct |
73 ms |
9464 KB |
Output is correct |
65 |
Correct |
6 ms |
640 KB |
Output is correct |
66 |
Correct |
93 ms |
10016 KB |
Output is correct |
67 |
Correct |
6 ms |
640 KB |
Output is correct |
68 |
Correct |
6 ms |
640 KB |
Output is correct |
69 |
Correct |
56 ms |
9712 KB |
Output is correct |
70 |
Correct |
6 ms |
768 KB |
Output is correct |
71 |
Correct |
70 ms |
9328 KB |
Output is correct |
72 |
Correct |
87 ms |
11276 KB |
Output is correct |
73 |
Correct |
5 ms |
384 KB |
Output is correct |
74 |
Correct |
92 ms |
15508 KB |
Output is correct |
75 |
Correct |
77 ms |
12412 KB |
Output is correct |
76 |
Correct |
6 ms |
640 KB |
Output is correct |
77 |
Correct |
6 ms |
640 KB |
Output is correct |
78 |
Correct |
55 ms |
9200 KB |
Output is correct |
79 |
Correct |
75 ms |
14192 KB |
Output is correct |
80 |
Correct |
7 ms |
768 KB |
Output is correct |
81 |
Correct |
6 ms |
640 KB |
Output is correct |
82 |
Correct |
5 ms |
384 KB |
Output is correct |
83 |
Correct |
5 ms |
384 KB |
Output is correct |
84 |
Correct |
6 ms |
640 KB |
Output is correct |
85 |
Correct |
93 ms |
11640 KB |
Output is correct |
86 |
Correct |
6 ms |
640 KB |
Output is correct |
87 |
Correct |
77 ms |
9464 KB |
Output is correct |
88 |
Correct |
5 ms |
384 KB |
Output is correct |
89 |
Correct |
85 ms |
9464 KB |
Output is correct |
90 |
Correct |
91 ms |
11640 KB |
Output is correct |
91 |
Correct |
87 ms |
14332 KB |
Output is correct |
92 |
Correct |
53 ms |
9712 KB |
Output is correct |
93 |
Correct |
6 ms |
640 KB |
Output is correct |
94 |
Correct |
5 ms |
384 KB |
Output is correct |
95 |
Correct |
74 ms |
14192 KB |
Output is correct |
96 |
Correct |
98 ms |
17528 KB |
Output is correct |
97 |
Correct |
6 ms |
640 KB |
Output is correct |
98 |
Correct |
4 ms |
384 KB |
Output is correct |
99 |
Correct |
70 ms |
12144 KB |
Output is correct |
100 |
Correct |
6 ms |
640 KB |
Output is correct |
101 |
Correct |
100 ms |
10364 KB |
Output is correct |
102 |
Correct |
89 ms |
16760 KB |
Output is correct |
103 |
Correct |
6 ms |
768 KB |
Output is correct |
104 |
Correct |
7 ms |
768 KB |
Output is correct |
105 |
Correct |
6 ms |
640 KB |
Output is correct |
106 |
Correct |
6 ms |
640 KB |
Output is correct |
107 |
Correct |
6 ms |
640 KB |
Output is correct |
108 |
Correct |
551 ms |
58620 KB |
Output is correct |
109 |
Correct |
671 ms |
60084 KB |
Output is correct |
110 |
Correct |
675 ms |
69224 KB |
Output is correct |
111 |
Correct |
739 ms |
91896 KB |
Output is correct |
112 |
Correct |
757 ms |
83960 KB |
Output is correct |
113 |
Correct |
523 ms |
72288 KB |
Output is correct |
114 |
Correct |
579 ms |
46228 KB |
Output is correct |
115 |
Correct |
575 ms |
45800 KB |
Output is correct |
116 |
Correct |
692 ms |
52856 KB |
Output is correct |
117 |
Correct |
677 ms |
73592 KB |
Output is correct |
118 |
Correct |
613 ms |
47048 KB |
Output is correct |
119 |
Correct |
702 ms |
73592 KB |
Output is correct |
120 |
Correct |
709 ms |
84580 KB |
Output is correct |
121 |
Correct |
663 ms |
73464 KB |
Output is correct |
122 |
Correct |
752 ms |
62612 KB |
Output is correct |
123 |
Correct |
495 ms |
75240 KB |
Output is correct |
124 |
Correct |
730 ms |
71800 KB |
Output is correct |