#include <bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
*/
using namespace std;
// using namespace __gnu_pbds;
using namespace chrono;
// mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
/*
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
//***************** CONSTANTS *****************
const int MXN = 500'000;
//***************** GLOBAL VARIABLES *****************
vector<int> g[MXN];
map<int, int> mp[MXN];
int deg[MXN], st[MXN], cnt[MXN];
//***************** AUXILIARY STRUCTS *****************
//***************** MAIN BODY *****************
void dfs(int u, int p){
if(cnt[st[u]] != 1) mp[u][st[u]] = 1;
for(int v : g[u]) if(v != p){
dfs(v, u);
if(!mp[v].size()){
deg[u]++;
deg[v]++;
}
else{
deg[u] += deg[v];
deg[v] = 0;
}
if(mp[v].size() > mp[u].size()) swap(mp[u], mp[v]);
for(auto& [state, count] : mp[v]){
mp[u][state] += count;
if(mp[u][state] == cnt[state]) mp[u].erase(state);
}
}
}
void solve(){
int N, K;
cin >> N >> K;
for(int i = 0; i < N - 1; i++){
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 0; i < N; i++){
cin >> st[i];
--st[i];
cnt[st[i]]++;
}
dfs(0, 0);
int ans = 0;
for(int i = 0; i < N; i++) ans += (deg[i] == 1);
cout << (ans + 1) / 2 << '\n';
}
//***************** *****************
int32_t main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
#ifdef LOCAL
auto begin = high_resolution_clock::now();
#endif
int tc = 1;
// cin >> tc;
for (int t = 0; t < tc; t++)
solve();
#ifdef LOCAL
auto end = high_resolution_clock::now();
cout << fixed << setprecision(4);
cout << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
#endif
return 0;
}
/*
If code gives a WA, check for the following :
1. I/O format
2. Are you clearing all global variables in between tests if multitests are a thing
3. Can you definitively prove the logic
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35532 KB |
Output is correct |
2 |
Correct |
20 ms |
35544 KB |
Output is correct |
3 |
Correct |
19 ms |
35504 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35532 KB |
Output is correct |
6 |
Correct |
19 ms |
35460 KB |
Output is correct |
7 |
Correct |
20 ms |
35468 KB |
Output is correct |
8 |
Correct |
19 ms |
35532 KB |
Output is correct |
9 |
Correct |
19 ms |
35512 KB |
Output is correct |
10 |
Correct |
20 ms |
35464 KB |
Output is correct |
11 |
Correct |
20 ms |
35512 KB |
Output is correct |
12 |
Correct |
20 ms |
35464 KB |
Output is correct |
13 |
Correct |
19 ms |
35464 KB |
Output is correct |
14 |
Correct |
20 ms |
35496 KB |
Output is correct |
15 |
Correct |
20 ms |
35476 KB |
Output is correct |
16 |
Correct |
18 ms |
35532 KB |
Output is correct |
17 |
Correct |
20 ms |
35548 KB |
Output is correct |
18 |
Correct |
19 ms |
35540 KB |
Output is correct |
19 |
Correct |
19 ms |
35532 KB |
Output is correct |
20 |
Correct |
19 ms |
35540 KB |
Output is correct |
21 |
Correct |
19 ms |
35480 KB |
Output is correct |
22 |
Correct |
19 ms |
35468 KB |
Output is correct |
23 |
Correct |
19 ms |
35532 KB |
Output is correct |
24 |
Correct |
19 ms |
35532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35532 KB |
Output is correct |
2 |
Correct |
20 ms |
35544 KB |
Output is correct |
3 |
Correct |
19 ms |
35504 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35532 KB |
Output is correct |
6 |
Correct |
19 ms |
35460 KB |
Output is correct |
7 |
Correct |
20 ms |
35468 KB |
Output is correct |
8 |
Correct |
19 ms |
35532 KB |
Output is correct |
9 |
Correct |
19 ms |
35512 KB |
Output is correct |
10 |
Correct |
20 ms |
35464 KB |
Output is correct |
11 |
Correct |
20 ms |
35512 KB |
Output is correct |
12 |
Correct |
20 ms |
35464 KB |
Output is correct |
13 |
Correct |
19 ms |
35464 KB |
Output is correct |
14 |
Correct |
20 ms |
35496 KB |
Output is correct |
15 |
Correct |
20 ms |
35476 KB |
Output is correct |
16 |
Correct |
18 ms |
35532 KB |
Output is correct |
17 |
Correct |
20 ms |
35548 KB |
Output is correct |
18 |
Correct |
19 ms |
35540 KB |
Output is correct |
19 |
Correct |
19 ms |
35532 KB |
Output is correct |
20 |
Correct |
19 ms |
35540 KB |
Output is correct |
21 |
Correct |
19 ms |
35480 KB |
Output is correct |
22 |
Correct |
19 ms |
35468 KB |
Output is correct |
23 |
Correct |
19 ms |
35532 KB |
Output is correct |
24 |
Correct |
19 ms |
35532 KB |
Output is correct |
25 |
Correct |
21 ms |
35532 KB |
Output is correct |
26 |
Correct |
21 ms |
35764 KB |
Output is correct |
27 |
Correct |
20 ms |
35828 KB |
Output is correct |
28 |
Correct |
22 ms |
35808 KB |
Output is correct |
29 |
Correct |
21 ms |
35660 KB |
Output is correct |
30 |
Correct |
23 ms |
36044 KB |
Output is correct |
31 |
Correct |
20 ms |
35524 KB |
Output is correct |
32 |
Correct |
20 ms |
35920 KB |
Output is correct |
33 |
Correct |
19 ms |
35532 KB |
Output is correct |
34 |
Correct |
22 ms |
35820 KB |
Output is correct |
35 |
Correct |
21 ms |
35688 KB |
Output is correct |
36 |
Correct |
22 ms |
35844 KB |
Output is correct |
37 |
Correct |
21 ms |
35908 KB |
Output is correct |
38 |
Correct |
20 ms |
35540 KB |
Output is correct |
39 |
Correct |
23 ms |
36072 KB |
Output is correct |
40 |
Correct |
20 ms |
35836 KB |
Output is correct |
41 |
Correct |
22 ms |
35856 KB |
Output is correct |
42 |
Correct |
36 ms |
35796 KB |
Output is correct |
43 |
Correct |
21 ms |
36224 KB |
Output is correct |
44 |
Correct |
20 ms |
35532 KB |
Output is correct |
45 |
Correct |
22 ms |
35740 KB |
Output is correct |
46 |
Correct |
26 ms |
35856 KB |
Output is correct |
47 |
Correct |
19 ms |
35532 KB |
Output is correct |
48 |
Correct |
21 ms |
35812 KB |
Output is correct |
49 |
Correct |
21 ms |
35676 KB |
Output is correct |
50 |
Correct |
20 ms |
35788 KB |
Output is correct |
51 |
Correct |
22 ms |
35808 KB |
Output is correct |
52 |
Correct |
21 ms |
35780 KB |
Output is correct |
53 |
Correct |
22 ms |
35808 KB |
Output is correct |
54 |
Correct |
22 ms |
36072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35532 KB |
Output is correct |
2 |
Correct |
20 ms |
35544 KB |
Output is correct |
3 |
Correct |
19 ms |
35504 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35532 KB |
Output is correct |
6 |
Correct |
19 ms |
35460 KB |
Output is correct |
7 |
Correct |
20 ms |
35468 KB |
Output is correct |
8 |
Correct |
19 ms |
35532 KB |
Output is correct |
9 |
Correct |
19 ms |
35512 KB |
Output is correct |
10 |
Correct |
20 ms |
35464 KB |
Output is correct |
11 |
Correct |
20 ms |
35512 KB |
Output is correct |
12 |
Correct |
20 ms |
35464 KB |
Output is correct |
13 |
Correct |
19 ms |
35464 KB |
Output is correct |
14 |
Correct |
20 ms |
35496 KB |
Output is correct |
15 |
Correct |
20 ms |
35476 KB |
Output is correct |
16 |
Correct |
18 ms |
35532 KB |
Output is correct |
17 |
Correct |
20 ms |
35548 KB |
Output is correct |
18 |
Correct |
19 ms |
35540 KB |
Output is correct |
19 |
Correct |
19 ms |
35532 KB |
Output is correct |
20 |
Correct |
19 ms |
35540 KB |
Output is correct |
21 |
Correct |
19 ms |
35480 KB |
Output is correct |
22 |
Correct |
19 ms |
35468 KB |
Output is correct |
23 |
Correct |
19 ms |
35532 KB |
Output is correct |
24 |
Correct |
19 ms |
35532 KB |
Output is correct |
25 |
Correct |
20 ms |
35456 KB |
Output is correct |
26 |
Correct |
88 ms |
45688 KB |
Output is correct |
27 |
Correct |
137 ms |
52036 KB |
Output is correct |
28 |
Correct |
21 ms |
35816 KB |
Output is correct |
29 |
Correct |
20 ms |
35476 KB |
Output is correct |
30 |
Correct |
20 ms |
35532 KB |
Output is correct |
31 |
Correct |
104 ms |
45416 KB |
Output is correct |
32 |
Correct |
22 ms |
35788 KB |
Output is correct |
33 |
Correct |
127 ms |
56960 KB |
Output is correct |
34 |
Correct |
156 ms |
50720 KB |
Output is correct |
35 |
Correct |
22 ms |
35812 KB |
Output is correct |
36 |
Correct |
106 ms |
47044 KB |
Output is correct |
37 |
Correct |
21 ms |
35788 KB |
Output is correct |
38 |
Correct |
21 ms |
35840 KB |
Output is correct |
39 |
Correct |
94 ms |
45952 KB |
Output is correct |
40 |
Correct |
22 ms |
36224 KB |
Output is correct |
41 |
Correct |
114 ms |
45528 KB |
Output is correct |
42 |
Correct |
125 ms |
49464 KB |
Output is correct |
43 |
Correct |
20 ms |
35532 KB |
Output is correct |
44 |
Correct |
114 ms |
57576 KB |
Output is correct |
45 |
Correct |
127 ms |
52816 KB |
Output is correct |
46 |
Correct |
22 ms |
35784 KB |
Output is correct |
47 |
Correct |
22 ms |
35940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
45696 KB |
Output is correct |
2 |
Correct |
71 ms |
41432 KB |
Output is correct |
3 |
Correct |
22 ms |
35660 KB |
Output is correct |
4 |
Correct |
22 ms |
35788 KB |
Output is correct |
5 |
Correct |
19 ms |
35436 KB |
Output is correct |
6 |
Correct |
19 ms |
35556 KB |
Output is correct |
7 |
Correct |
22 ms |
35792 KB |
Output is correct |
8 |
Correct |
129 ms |
47908 KB |
Output is correct |
9 |
Correct |
21 ms |
35788 KB |
Output is correct |
10 |
Correct |
99 ms |
45636 KB |
Output is correct |
11 |
Correct |
20 ms |
35548 KB |
Output is correct |
12 |
Correct |
106 ms |
45728 KB |
Output is correct |
13 |
Correct |
105 ms |
45160 KB |
Output is correct |
14 |
Correct |
84 ms |
41608 KB |
Output is correct |
15 |
Correct |
90 ms |
45880 KB |
Output is correct |
16 |
Correct |
24 ms |
35864 KB |
Output is correct |
17 |
Correct |
21 ms |
35448 KB |
Output is correct |
18 |
Correct |
81 ms |
43008 KB |
Output is correct |
19 |
Correct |
92 ms |
47940 KB |
Output is correct |
20 |
Correct |
21 ms |
35804 KB |
Output is correct |
21 |
Correct |
20 ms |
35576 KB |
Output is correct |
22 |
Correct |
130 ms |
45452 KB |
Output is correct |
23 |
Correct |
21 ms |
35788 KB |
Output is correct |
24 |
Correct |
124 ms |
45304 KB |
Output is correct |
25 |
Correct |
90 ms |
45872 KB |
Output is correct |
26 |
Correct |
21 ms |
35660 KB |
Output is correct |
27 |
Correct |
21 ms |
35788 KB |
Output is correct |
28 |
Correct |
21 ms |
35808 KB |
Output is correct |
29 |
Correct |
23 ms |
35880 KB |
Output is correct |
30 |
Correct |
21 ms |
35764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35532 KB |
Output is correct |
2 |
Correct |
20 ms |
35544 KB |
Output is correct |
3 |
Correct |
19 ms |
35504 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35532 KB |
Output is correct |
6 |
Correct |
19 ms |
35460 KB |
Output is correct |
7 |
Correct |
20 ms |
35468 KB |
Output is correct |
8 |
Correct |
19 ms |
35532 KB |
Output is correct |
9 |
Correct |
19 ms |
35512 KB |
Output is correct |
10 |
Correct |
20 ms |
35464 KB |
Output is correct |
11 |
Correct |
20 ms |
35512 KB |
Output is correct |
12 |
Correct |
20 ms |
35464 KB |
Output is correct |
13 |
Correct |
19 ms |
35464 KB |
Output is correct |
14 |
Correct |
20 ms |
35496 KB |
Output is correct |
15 |
Correct |
20 ms |
35476 KB |
Output is correct |
16 |
Correct |
18 ms |
35532 KB |
Output is correct |
17 |
Correct |
20 ms |
35548 KB |
Output is correct |
18 |
Correct |
19 ms |
35540 KB |
Output is correct |
19 |
Correct |
19 ms |
35532 KB |
Output is correct |
20 |
Correct |
19 ms |
35540 KB |
Output is correct |
21 |
Correct |
19 ms |
35480 KB |
Output is correct |
22 |
Correct |
19 ms |
35468 KB |
Output is correct |
23 |
Correct |
19 ms |
35532 KB |
Output is correct |
24 |
Correct |
19 ms |
35532 KB |
Output is correct |
25 |
Correct |
21 ms |
35532 KB |
Output is correct |
26 |
Correct |
21 ms |
35764 KB |
Output is correct |
27 |
Correct |
20 ms |
35828 KB |
Output is correct |
28 |
Correct |
22 ms |
35808 KB |
Output is correct |
29 |
Correct |
21 ms |
35660 KB |
Output is correct |
30 |
Correct |
23 ms |
36044 KB |
Output is correct |
31 |
Correct |
20 ms |
35524 KB |
Output is correct |
32 |
Correct |
20 ms |
35920 KB |
Output is correct |
33 |
Correct |
19 ms |
35532 KB |
Output is correct |
34 |
Correct |
22 ms |
35820 KB |
Output is correct |
35 |
Correct |
21 ms |
35688 KB |
Output is correct |
36 |
Correct |
22 ms |
35844 KB |
Output is correct |
37 |
Correct |
21 ms |
35908 KB |
Output is correct |
38 |
Correct |
20 ms |
35540 KB |
Output is correct |
39 |
Correct |
23 ms |
36072 KB |
Output is correct |
40 |
Correct |
20 ms |
35836 KB |
Output is correct |
41 |
Correct |
22 ms |
35856 KB |
Output is correct |
42 |
Correct |
36 ms |
35796 KB |
Output is correct |
43 |
Correct |
21 ms |
36224 KB |
Output is correct |
44 |
Correct |
20 ms |
35532 KB |
Output is correct |
45 |
Correct |
22 ms |
35740 KB |
Output is correct |
46 |
Correct |
26 ms |
35856 KB |
Output is correct |
47 |
Correct |
19 ms |
35532 KB |
Output is correct |
48 |
Correct |
21 ms |
35812 KB |
Output is correct |
49 |
Correct |
21 ms |
35676 KB |
Output is correct |
50 |
Correct |
20 ms |
35788 KB |
Output is correct |
51 |
Correct |
22 ms |
35808 KB |
Output is correct |
52 |
Correct |
21 ms |
35780 KB |
Output is correct |
53 |
Correct |
22 ms |
35808 KB |
Output is correct |
54 |
Correct |
22 ms |
36072 KB |
Output is correct |
55 |
Correct |
20 ms |
35456 KB |
Output is correct |
56 |
Correct |
88 ms |
45688 KB |
Output is correct |
57 |
Correct |
137 ms |
52036 KB |
Output is correct |
58 |
Correct |
21 ms |
35816 KB |
Output is correct |
59 |
Correct |
20 ms |
35476 KB |
Output is correct |
60 |
Correct |
20 ms |
35532 KB |
Output is correct |
61 |
Correct |
104 ms |
45416 KB |
Output is correct |
62 |
Correct |
22 ms |
35788 KB |
Output is correct |
63 |
Correct |
127 ms |
56960 KB |
Output is correct |
64 |
Correct |
156 ms |
50720 KB |
Output is correct |
65 |
Correct |
22 ms |
35812 KB |
Output is correct |
66 |
Correct |
106 ms |
47044 KB |
Output is correct |
67 |
Correct |
21 ms |
35788 KB |
Output is correct |
68 |
Correct |
21 ms |
35840 KB |
Output is correct |
69 |
Correct |
94 ms |
45952 KB |
Output is correct |
70 |
Correct |
22 ms |
36224 KB |
Output is correct |
71 |
Correct |
114 ms |
45528 KB |
Output is correct |
72 |
Correct |
125 ms |
49464 KB |
Output is correct |
73 |
Correct |
20 ms |
35532 KB |
Output is correct |
74 |
Correct |
114 ms |
57576 KB |
Output is correct |
75 |
Correct |
127 ms |
52816 KB |
Output is correct |
76 |
Correct |
22 ms |
35784 KB |
Output is correct |
77 |
Correct |
22 ms |
35940 KB |
Output is correct |
78 |
Correct |
89 ms |
45696 KB |
Output is correct |
79 |
Correct |
71 ms |
41432 KB |
Output is correct |
80 |
Correct |
22 ms |
35660 KB |
Output is correct |
81 |
Correct |
22 ms |
35788 KB |
Output is correct |
82 |
Correct |
19 ms |
35436 KB |
Output is correct |
83 |
Correct |
19 ms |
35556 KB |
Output is correct |
84 |
Correct |
22 ms |
35792 KB |
Output is correct |
85 |
Correct |
129 ms |
47908 KB |
Output is correct |
86 |
Correct |
21 ms |
35788 KB |
Output is correct |
87 |
Correct |
99 ms |
45636 KB |
Output is correct |
88 |
Correct |
20 ms |
35548 KB |
Output is correct |
89 |
Correct |
106 ms |
45728 KB |
Output is correct |
90 |
Correct |
105 ms |
45160 KB |
Output is correct |
91 |
Correct |
84 ms |
41608 KB |
Output is correct |
92 |
Correct |
90 ms |
45880 KB |
Output is correct |
93 |
Correct |
24 ms |
35864 KB |
Output is correct |
94 |
Correct |
21 ms |
35448 KB |
Output is correct |
95 |
Correct |
81 ms |
43008 KB |
Output is correct |
96 |
Correct |
92 ms |
47940 KB |
Output is correct |
97 |
Correct |
21 ms |
35804 KB |
Output is correct |
98 |
Correct |
20 ms |
35576 KB |
Output is correct |
99 |
Correct |
130 ms |
45452 KB |
Output is correct |
100 |
Correct |
21 ms |
35788 KB |
Output is correct |
101 |
Correct |
124 ms |
45304 KB |
Output is correct |
102 |
Correct |
90 ms |
45872 KB |
Output is correct |
103 |
Correct |
21 ms |
35660 KB |
Output is correct |
104 |
Correct |
21 ms |
35788 KB |
Output is correct |
105 |
Correct |
21 ms |
35808 KB |
Output is correct |
106 |
Correct |
23 ms |
35880 KB |
Output is correct |
107 |
Correct |
21 ms |
35764 KB |
Output is correct |
108 |
Correct |
884 ms |
81804 KB |
Output is correct |
109 |
Correct |
726 ms |
110808 KB |
Output is correct |
110 |
Correct |
765 ms |
128728 KB |
Output is correct |
111 |
Correct |
568 ms |
97120 KB |
Output is correct |
112 |
Correct |
597 ms |
81604 KB |
Output is correct |
113 |
Correct |
458 ms |
65844 KB |
Output is correct |
114 |
Correct |
666 ms |
81796 KB |
Output is correct |
115 |
Correct |
675 ms |
81612 KB |
Output is correct |
116 |
Correct |
1098 ms |
139136 KB |
Output is correct |
117 |
Correct |
536 ms |
60028 KB |
Output is correct |
118 |
Correct |
653 ms |
80708 KB |
Output is correct |
119 |
Correct |
524 ms |
60032 KB |
Output is correct |
120 |
Correct |
567 ms |
82604 KB |
Output is correct |
121 |
Correct |
530 ms |
60164 KB |
Output is correct |
122 |
Correct |
1180 ms |
147144 KB |
Output is correct |
123 |
Correct |
376 ms |
61520 KB |
Output is correct |
124 |
Correct |
602 ms |
59844 KB |
Output is correct |