#include "bits/stdc++.h"
#define int long long
using namespace std;
const int MAXN = 5e5 + 10;
vector<int> adj[MAXN];
vector<int> adj2[MAXN];
int s[MAXN];
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
int dfs(int node, int prev, int q) {
//stat: do the ancestors contain color q
int cnt = (s[node] == q);
for(int x: adj[node]) {
if(x != prev) {
int d = dfs(x, node, q);
cnt += d;
if(d > 0 && set_of(node) != set_of(x)) {
union_(node, x);
}
}
}
//ret: how many color q in subtree
return cnt;
}
int32_t main() {
int n,k;cin >> n >> k;
for(int i=1;i<n;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 >> s[i];
for(int i=0; i<=n; i++) dsu[i] = i;
for(int i=1; i<=k; i++) {
for(int j=1; j<=n; j++) {
if(s[j] == i) {
dfs(j, -1, i);
break;
}
}
}
map<pair<int, int>, bool> mp;
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(set_of(i) != set_of(x) && mp[{set_of(i), set_of(x)}] == 0) {
mp[{set_of(i), set_of(x)}] = 1;
mp[{set_of(x), set_of(i)}] = 1;
adj2[set_of(i)].push_back(set_of(x));
adj2[set_of(x)].push_back(set_of(i));
}
}
}
int s = 0;
for(int i=1; i<=n; i++) {
s += (adj2[i].size() == 1);
}
cout << (s + 1) / 2 << "\n";
}
/*
6 5
1 2
2 3
2 4
3 5
2 6
1 5 2 4 2 3
myAnswer 3
correctAnswer 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23672 KB |
Output is correct |
2 |
Correct |
13 ms |
23760 KB |
Output is correct |
3 |
Correct |
13 ms |
23708 KB |
Output is correct |
4 |
Correct |
14 ms |
23792 KB |
Output is correct |
5 |
Correct |
13 ms |
23688 KB |
Output is correct |
6 |
Correct |
14 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23752 KB |
Output is correct |
8 |
Correct |
13 ms |
23788 KB |
Output is correct |
9 |
Correct |
11 ms |
23740 KB |
Output is correct |
10 |
Correct |
11 ms |
23704 KB |
Output is correct |
11 |
Correct |
12 ms |
23680 KB |
Output is correct |
12 |
Correct |
14 ms |
23904 KB |
Output is correct |
13 |
Correct |
13 ms |
23692 KB |
Output is correct |
14 |
Correct |
12 ms |
23792 KB |
Output is correct |
15 |
Correct |
14 ms |
23792 KB |
Output is correct |
16 |
Correct |
15 ms |
23792 KB |
Output is correct |
17 |
Correct |
12 ms |
23760 KB |
Output is correct |
18 |
Correct |
14 ms |
23760 KB |
Output is correct |
19 |
Correct |
12 ms |
23784 KB |
Output is correct |
20 |
Correct |
12 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23760 KB |
Output is correct |
22 |
Correct |
14 ms |
23792 KB |
Output is correct |
23 |
Correct |
12 ms |
23684 KB |
Output is correct |
24 |
Correct |
13 ms |
23792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23672 KB |
Output is correct |
2 |
Correct |
13 ms |
23760 KB |
Output is correct |
3 |
Correct |
13 ms |
23708 KB |
Output is correct |
4 |
Correct |
14 ms |
23792 KB |
Output is correct |
5 |
Correct |
13 ms |
23688 KB |
Output is correct |
6 |
Correct |
14 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23752 KB |
Output is correct |
8 |
Correct |
13 ms |
23788 KB |
Output is correct |
9 |
Correct |
11 ms |
23740 KB |
Output is correct |
10 |
Correct |
11 ms |
23704 KB |
Output is correct |
11 |
Correct |
12 ms |
23680 KB |
Output is correct |
12 |
Correct |
14 ms |
23904 KB |
Output is correct |
13 |
Correct |
13 ms |
23692 KB |
Output is correct |
14 |
Correct |
12 ms |
23792 KB |
Output is correct |
15 |
Correct |
14 ms |
23792 KB |
Output is correct |
16 |
Correct |
15 ms |
23792 KB |
Output is correct |
17 |
Correct |
12 ms |
23760 KB |
Output is correct |
18 |
Correct |
14 ms |
23760 KB |
Output is correct |
19 |
Correct |
12 ms |
23784 KB |
Output is correct |
20 |
Correct |
12 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23760 KB |
Output is correct |
22 |
Correct |
14 ms |
23792 KB |
Output is correct |
23 |
Correct |
12 ms |
23684 KB |
Output is correct |
24 |
Correct |
13 ms |
23792 KB |
Output is correct |
25 |
Correct |
13 ms |
23760 KB |
Output is correct |
26 |
Correct |
122 ms |
24232 KB |
Output is correct |
27 |
Correct |
22 ms |
23980 KB |
Output is correct |
28 |
Correct |
153 ms |
24392 KB |
Output is correct |
29 |
Correct |
142 ms |
24392 KB |
Output is correct |
30 |
Correct |
23 ms |
24044 KB |
Output is correct |
31 |
Correct |
14 ms |
23788 KB |
Output is correct |
32 |
Correct |
187 ms |
24188 KB |
Output is correct |
33 |
Correct |
16 ms |
23760 KB |
Output is correct |
34 |
Correct |
16 ms |
23972 KB |
Output is correct |
35 |
Correct |
136 ms |
24404 KB |
Output is correct |
36 |
Correct |
17 ms |
23888 KB |
Output is correct |
37 |
Correct |
75 ms |
24008 KB |
Output is correct |
38 |
Correct |
13 ms |
23760 KB |
Output is correct |
39 |
Correct |
77 ms |
23960 KB |
Output is correct |
40 |
Correct |
15 ms |
23888 KB |
Output is correct |
41 |
Correct |
15 ms |
23860 KB |
Output is correct |
42 |
Correct |
43 ms |
23988 KB |
Output is correct |
43 |
Correct |
22 ms |
24128 KB |
Output is correct |
44 |
Correct |
15 ms |
23768 KB |
Output is correct |
45 |
Correct |
171 ms |
24028 KB |
Output is correct |
46 |
Correct |
78 ms |
23988 KB |
Output is correct |
47 |
Correct |
13 ms |
23760 KB |
Output is correct |
48 |
Correct |
43 ms |
24032 KB |
Output is correct |
49 |
Correct |
75 ms |
24368 KB |
Output is correct |
50 |
Correct |
171 ms |
24500 KB |
Output is correct |
51 |
Correct |
43 ms |
23972 KB |
Output is correct |
52 |
Correct |
15 ms |
23888 KB |
Output is correct |
53 |
Correct |
39 ms |
24008 KB |
Output is correct |
54 |
Correct |
19 ms |
23992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23672 KB |
Output is correct |
2 |
Correct |
13 ms |
23760 KB |
Output is correct |
3 |
Correct |
13 ms |
23708 KB |
Output is correct |
4 |
Correct |
14 ms |
23792 KB |
Output is correct |
5 |
Correct |
13 ms |
23688 KB |
Output is correct |
6 |
Correct |
14 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23752 KB |
Output is correct |
8 |
Correct |
13 ms |
23788 KB |
Output is correct |
9 |
Correct |
11 ms |
23740 KB |
Output is correct |
10 |
Correct |
11 ms |
23704 KB |
Output is correct |
11 |
Correct |
12 ms |
23680 KB |
Output is correct |
12 |
Correct |
14 ms |
23904 KB |
Output is correct |
13 |
Correct |
13 ms |
23692 KB |
Output is correct |
14 |
Correct |
12 ms |
23792 KB |
Output is correct |
15 |
Correct |
14 ms |
23792 KB |
Output is correct |
16 |
Correct |
15 ms |
23792 KB |
Output is correct |
17 |
Correct |
12 ms |
23760 KB |
Output is correct |
18 |
Correct |
14 ms |
23760 KB |
Output is correct |
19 |
Correct |
12 ms |
23784 KB |
Output is correct |
20 |
Correct |
12 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23760 KB |
Output is correct |
22 |
Correct |
14 ms |
23792 KB |
Output is correct |
23 |
Correct |
12 ms |
23684 KB |
Output is correct |
24 |
Correct |
13 ms |
23792 KB |
Output is correct |
25 |
Correct |
15 ms |
23796 KB |
Output is correct |
26 |
Correct |
160 ms |
31104 KB |
Output is correct |
27 |
Correct |
384 ms |
30440 KB |
Output is correct |
28 |
Correct |
18 ms |
23932 KB |
Output is correct |
29 |
Correct |
16 ms |
23728 KB |
Output is correct |
30 |
Correct |
13 ms |
23796 KB |
Output is correct |
31 |
Correct |
331 ms |
30516 KB |
Output is correct |
32 |
Correct |
15 ms |
24016 KB |
Output is correct |
33 |
Correct |
745 ms |
37576 KB |
Output is correct |
34 |
Correct |
480 ms |
30528 KB |
Output is correct |
35 |
Correct |
18 ms |
23888 KB |
Output is correct |
36 |
Correct |
476 ms |
30972 KB |
Output is correct |
37 |
Correct |
16 ms |
23888 KB |
Output is correct |
38 |
Correct |
15 ms |
23932 KB |
Output is correct |
39 |
Correct |
141 ms |
30672 KB |
Output is correct |
40 |
Correct |
14 ms |
24136 KB |
Output is correct |
41 |
Correct |
118 ms |
30380 KB |
Output is correct |
42 |
Correct |
716 ms |
34712 KB |
Output is correct |
43 |
Correct |
14 ms |
23760 KB |
Output is correct |
44 |
Correct |
118 ms |
37360 KB |
Output is correct |
45 |
Correct |
566 ms |
34880 KB |
Output is correct |
46 |
Correct |
17 ms |
24016 KB |
Output is correct |
47 |
Correct |
24 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
31192 KB |
Output is correct |
2 |
Execution timed out |
3095 ms |
30968 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23672 KB |
Output is correct |
2 |
Correct |
13 ms |
23760 KB |
Output is correct |
3 |
Correct |
13 ms |
23708 KB |
Output is correct |
4 |
Correct |
14 ms |
23792 KB |
Output is correct |
5 |
Correct |
13 ms |
23688 KB |
Output is correct |
6 |
Correct |
14 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23752 KB |
Output is correct |
8 |
Correct |
13 ms |
23788 KB |
Output is correct |
9 |
Correct |
11 ms |
23740 KB |
Output is correct |
10 |
Correct |
11 ms |
23704 KB |
Output is correct |
11 |
Correct |
12 ms |
23680 KB |
Output is correct |
12 |
Correct |
14 ms |
23904 KB |
Output is correct |
13 |
Correct |
13 ms |
23692 KB |
Output is correct |
14 |
Correct |
12 ms |
23792 KB |
Output is correct |
15 |
Correct |
14 ms |
23792 KB |
Output is correct |
16 |
Correct |
15 ms |
23792 KB |
Output is correct |
17 |
Correct |
12 ms |
23760 KB |
Output is correct |
18 |
Correct |
14 ms |
23760 KB |
Output is correct |
19 |
Correct |
12 ms |
23784 KB |
Output is correct |
20 |
Correct |
12 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23760 KB |
Output is correct |
22 |
Correct |
14 ms |
23792 KB |
Output is correct |
23 |
Correct |
12 ms |
23684 KB |
Output is correct |
24 |
Correct |
13 ms |
23792 KB |
Output is correct |
25 |
Correct |
13 ms |
23760 KB |
Output is correct |
26 |
Correct |
122 ms |
24232 KB |
Output is correct |
27 |
Correct |
22 ms |
23980 KB |
Output is correct |
28 |
Correct |
153 ms |
24392 KB |
Output is correct |
29 |
Correct |
142 ms |
24392 KB |
Output is correct |
30 |
Correct |
23 ms |
24044 KB |
Output is correct |
31 |
Correct |
14 ms |
23788 KB |
Output is correct |
32 |
Correct |
187 ms |
24188 KB |
Output is correct |
33 |
Correct |
16 ms |
23760 KB |
Output is correct |
34 |
Correct |
16 ms |
23972 KB |
Output is correct |
35 |
Correct |
136 ms |
24404 KB |
Output is correct |
36 |
Correct |
17 ms |
23888 KB |
Output is correct |
37 |
Correct |
75 ms |
24008 KB |
Output is correct |
38 |
Correct |
13 ms |
23760 KB |
Output is correct |
39 |
Correct |
77 ms |
23960 KB |
Output is correct |
40 |
Correct |
15 ms |
23888 KB |
Output is correct |
41 |
Correct |
15 ms |
23860 KB |
Output is correct |
42 |
Correct |
43 ms |
23988 KB |
Output is correct |
43 |
Correct |
22 ms |
24128 KB |
Output is correct |
44 |
Correct |
15 ms |
23768 KB |
Output is correct |
45 |
Correct |
171 ms |
24028 KB |
Output is correct |
46 |
Correct |
78 ms |
23988 KB |
Output is correct |
47 |
Correct |
13 ms |
23760 KB |
Output is correct |
48 |
Correct |
43 ms |
24032 KB |
Output is correct |
49 |
Correct |
75 ms |
24368 KB |
Output is correct |
50 |
Correct |
171 ms |
24500 KB |
Output is correct |
51 |
Correct |
43 ms |
23972 KB |
Output is correct |
52 |
Correct |
15 ms |
23888 KB |
Output is correct |
53 |
Correct |
39 ms |
24008 KB |
Output is correct |
54 |
Correct |
19 ms |
23992 KB |
Output is correct |
55 |
Correct |
15 ms |
23796 KB |
Output is correct |
56 |
Correct |
160 ms |
31104 KB |
Output is correct |
57 |
Correct |
384 ms |
30440 KB |
Output is correct |
58 |
Correct |
18 ms |
23932 KB |
Output is correct |
59 |
Correct |
16 ms |
23728 KB |
Output is correct |
60 |
Correct |
13 ms |
23796 KB |
Output is correct |
61 |
Correct |
331 ms |
30516 KB |
Output is correct |
62 |
Correct |
15 ms |
24016 KB |
Output is correct |
63 |
Correct |
745 ms |
37576 KB |
Output is correct |
64 |
Correct |
480 ms |
30528 KB |
Output is correct |
65 |
Correct |
18 ms |
23888 KB |
Output is correct |
66 |
Correct |
476 ms |
30972 KB |
Output is correct |
67 |
Correct |
16 ms |
23888 KB |
Output is correct |
68 |
Correct |
15 ms |
23932 KB |
Output is correct |
69 |
Correct |
141 ms |
30672 KB |
Output is correct |
70 |
Correct |
14 ms |
24136 KB |
Output is correct |
71 |
Correct |
118 ms |
30380 KB |
Output is correct |
72 |
Correct |
716 ms |
34712 KB |
Output is correct |
73 |
Correct |
14 ms |
23760 KB |
Output is correct |
74 |
Correct |
118 ms |
37360 KB |
Output is correct |
75 |
Correct |
566 ms |
34880 KB |
Output is correct |
76 |
Correct |
17 ms |
24016 KB |
Output is correct |
77 |
Correct |
24 ms |
23888 KB |
Output is correct |
78 |
Correct |
136 ms |
31192 KB |
Output is correct |
79 |
Execution timed out |
3095 ms |
30968 KB |
Time limit exceeded |
80 |
Halted |
0 ms |
0 KB |
- |