#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 5e5 + 7;
int par[N];
int get(int a) {
return (a == par[a] ? a : par[a] = get(par[a]));
}
void join(int a, int b) {
a = get(a);
b = get(b);
par[a] = b;
}
vector <int> g[N];
mt19937_64 rnd(123);
int a[N], all[N], sz[N], bigChild[N], l[N], r[N];
ull go[N], sum[N];
vector <int> e;
void dfs(int u, int p) {
e.push_back(u);
l[u] = (int)e.size() - 1;
sum[u] += go[a[u]];
sz[u] = 1;
bigChild[u] = -1;
for (auto v : g[u]) {
if (v != p) {
dfs(v, u);
sum[u] += sum[v];
sz[u] += sz[v];
if (bigChild[u] == -1 || sz[v] > sz[bigChild[u]]) {
bigChild[u] = v;
}
}
}
r[u] = (int)e.size() - 1;
}
bool have[N];
ull need_sum = 0;
void jhfs(int u, int p, bool keep) {
for (auto v : g[u]) {
if (v != p && v != bigChild[u]) {
jhfs(v, u, 0);
}
}
if (bigChild[u] != -1) {
jhfs(bigChild[u], u, 1);
}
for (auto v : g[u]) {
if (v != p && v != bigChild[u]) {
for (int i = l[v]; i <= r[v]; ++i) {
int c = a[e[i]];
if (!have[c]) {
have[c] = 1;
need_sum += go[c] * all[c];
}
}
}
}
if (!have[a[u]]) {
have[a[u]] = 1;
need_sum += go[a[u]] * all[a[u]];
}
if (sum[u] != need_sum) {
join(u, p);
}
if (!keep) {
need_sum = 0;
for (int i = l[u]; i <= r[u]; ++i) {
int c = a[e[i]];
have[c] = 0;
}
}
}
int k;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n >> k;
for (int i = 0; i + 1 < n; ++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 >> a[i];
--a[i];
++all[a[i]];
}
for (int i = 0; i < k; ++i) go[i] = rnd();
for (int i = 0; i < n; ++i) par[i] = i;
dfs(0, -1);
jhfs(0, -1, 0);
vector <int> deg(n);
for (int i = 0; i < n; ++i) {
for (int j : g[i]) {
if (get(i) != get(j)) {
++deg[get(i)];
}
}
}
int cnt = 0;
for (int i = 0; i < n; ++i) if (deg[i] == 1) ++cnt;
cout << (cnt + 1) / 2 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
10 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12136 KB |
Output is correct |
5 |
Correct |
7 ms |
12160 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
7 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
7 ms |
12160 KB |
Output is correct |
13 |
Correct |
10 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
7 ms |
12160 KB |
Output is correct |
16 |
Correct |
10 ms |
12160 KB |
Output is correct |
17 |
Correct |
7 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
7 ms |
12160 KB |
Output is correct |
21 |
Correct |
10 ms |
12160 KB |
Output is correct |
22 |
Correct |
9 ms |
12160 KB |
Output is correct |
23 |
Correct |
7 ms |
12160 KB |
Output is correct |
24 |
Correct |
9 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
10 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12136 KB |
Output is correct |
5 |
Correct |
7 ms |
12160 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
7 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
7 ms |
12160 KB |
Output is correct |
13 |
Correct |
10 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
7 ms |
12160 KB |
Output is correct |
16 |
Correct |
10 ms |
12160 KB |
Output is correct |
17 |
Correct |
7 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
7 ms |
12160 KB |
Output is correct |
21 |
Correct |
10 ms |
12160 KB |
Output is correct |
22 |
Correct |
9 ms |
12160 KB |
Output is correct |
23 |
Correct |
7 ms |
12160 KB |
Output is correct |
24 |
Correct |
9 ms |
12160 KB |
Output is correct |
25 |
Correct |
7 ms |
12160 KB |
Output is correct |
26 |
Correct |
12 ms |
12416 KB |
Output is correct |
27 |
Correct |
9 ms |
12416 KB |
Output is correct |
28 |
Correct |
9 ms |
12544 KB |
Output is correct |
29 |
Correct |
9 ms |
12416 KB |
Output is correct |
30 |
Correct |
9 ms |
12416 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
13 ms |
12544 KB |
Output is correct |
33 |
Correct |
9 ms |
12160 KB |
Output is correct |
34 |
Correct |
10 ms |
12416 KB |
Output is correct |
35 |
Correct |
10 ms |
12416 KB |
Output is correct |
36 |
Correct |
9 ms |
12416 KB |
Output is correct |
37 |
Correct |
11 ms |
12416 KB |
Output is correct |
38 |
Correct |
7 ms |
12212 KB |
Output is correct |
39 |
Correct |
11 ms |
12416 KB |
Output is correct |
40 |
Correct |
9 ms |
12416 KB |
Output is correct |
41 |
Correct |
9 ms |
12416 KB |
Output is correct |
42 |
Correct |
9 ms |
12416 KB |
Output is correct |
43 |
Correct |
11 ms |
12544 KB |
Output is correct |
44 |
Correct |
8 ms |
12160 KB |
Output is correct |
45 |
Correct |
9 ms |
12416 KB |
Output is correct |
46 |
Correct |
13 ms |
12416 KB |
Output is correct |
47 |
Correct |
7 ms |
12160 KB |
Output is correct |
48 |
Correct |
9 ms |
12416 KB |
Output is correct |
49 |
Correct |
9 ms |
12416 KB |
Output is correct |
50 |
Correct |
9 ms |
12416 KB |
Output is correct |
51 |
Correct |
9 ms |
12416 KB |
Output is correct |
52 |
Correct |
9 ms |
12416 KB |
Output is correct |
53 |
Correct |
9 ms |
12416 KB |
Output is correct |
54 |
Correct |
9 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
10 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12136 KB |
Output is correct |
5 |
Correct |
7 ms |
12160 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
7 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
7 ms |
12160 KB |
Output is correct |
13 |
Correct |
10 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
7 ms |
12160 KB |
Output is correct |
16 |
Correct |
10 ms |
12160 KB |
Output is correct |
17 |
Correct |
7 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
7 ms |
12160 KB |
Output is correct |
21 |
Correct |
10 ms |
12160 KB |
Output is correct |
22 |
Correct |
9 ms |
12160 KB |
Output is correct |
23 |
Correct |
7 ms |
12160 KB |
Output is correct |
24 |
Correct |
9 ms |
12160 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
73 ms |
21104 KB |
Output is correct |
27 |
Correct |
112 ms |
20976 KB |
Output is correct |
28 |
Correct |
9 ms |
12416 KB |
Output is correct |
29 |
Correct |
10 ms |
12160 KB |
Output is correct |
30 |
Correct |
8 ms |
12160 KB |
Output is correct |
31 |
Correct |
95 ms |
20848 KB |
Output is correct |
32 |
Correct |
9 ms |
12416 KB |
Output is correct |
33 |
Correct |
100 ms |
28016 KB |
Output is correct |
34 |
Correct |
98 ms |
20892 KB |
Output is correct |
35 |
Correct |
9 ms |
12416 KB |
Output is correct |
36 |
Correct |
96 ms |
21616 KB |
Output is correct |
37 |
Correct |
10 ms |
12416 KB |
Output is correct |
38 |
Correct |
9 ms |
12416 KB |
Output is correct |
39 |
Correct |
66 ms |
21100 KB |
Output is correct |
40 |
Correct |
13 ms |
12672 KB |
Output is correct |
41 |
Correct |
114 ms |
20848 KB |
Output is correct |
42 |
Correct |
138 ms |
23152 KB |
Output is correct |
43 |
Correct |
9 ms |
12192 KB |
Output is correct |
44 |
Correct |
100 ms |
28272 KB |
Output is correct |
45 |
Correct |
107 ms |
24560 KB |
Output is correct |
46 |
Correct |
9 ms |
12416 KB |
Output is correct |
47 |
Correct |
11 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
21104 KB |
Output is correct |
2 |
Correct |
73 ms |
22768 KB |
Output is correct |
3 |
Correct |
10 ms |
12416 KB |
Output is correct |
4 |
Correct |
9 ms |
12416 KB |
Output is correct |
5 |
Correct |
10 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12416 KB |
Output is correct |
8 |
Correct |
108 ms |
21872 KB |
Output is correct |
9 |
Correct |
9 ms |
12416 KB |
Output is correct |
10 |
Correct |
107 ms |
20976 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
102 ms |
20976 KB |
Output is correct |
13 |
Correct |
109 ms |
21748 KB |
Output is correct |
14 |
Correct |
111 ms |
22508 KB |
Output is correct |
15 |
Correct |
84 ms |
21100 KB |
Output is correct |
16 |
Correct |
12 ms |
12416 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
85 ms |
22636 KB |
Output is correct |
19 |
Correct |
151 ms |
26352 KB |
Output is correct |
20 |
Correct |
14 ms |
12416 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
88 ms |
22000 KB |
Output is correct |
23 |
Correct |
13 ms |
12416 KB |
Output is correct |
24 |
Correct |
134 ms |
21100 KB |
Output is correct |
25 |
Correct |
132 ms |
25072 KB |
Output is correct |
26 |
Correct |
11 ms |
12416 KB |
Output is correct |
27 |
Correct |
11 ms |
12544 KB |
Output is correct |
28 |
Correct |
12 ms |
12416 KB |
Output is correct |
29 |
Correct |
11 ms |
12416 KB |
Output is correct |
30 |
Correct |
11 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
10 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12136 KB |
Output is correct |
5 |
Correct |
7 ms |
12160 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
7 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
7 ms |
12160 KB |
Output is correct |
13 |
Correct |
10 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
7 ms |
12160 KB |
Output is correct |
16 |
Correct |
10 ms |
12160 KB |
Output is correct |
17 |
Correct |
7 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
7 ms |
12160 KB |
Output is correct |
21 |
Correct |
10 ms |
12160 KB |
Output is correct |
22 |
Correct |
9 ms |
12160 KB |
Output is correct |
23 |
Correct |
7 ms |
12160 KB |
Output is correct |
24 |
Correct |
9 ms |
12160 KB |
Output is correct |
25 |
Correct |
7 ms |
12160 KB |
Output is correct |
26 |
Correct |
12 ms |
12416 KB |
Output is correct |
27 |
Correct |
9 ms |
12416 KB |
Output is correct |
28 |
Correct |
9 ms |
12544 KB |
Output is correct |
29 |
Correct |
9 ms |
12416 KB |
Output is correct |
30 |
Correct |
9 ms |
12416 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
13 ms |
12544 KB |
Output is correct |
33 |
Correct |
9 ms |
12160 KB |
Output is correct |
34 |
Correct |
10 ms |
12416 KB |
Output is correct |
35 |
Correct |
10 ms |
12416 KB |
Output is correct |
36 |
Correct |
9 ms |
12416 KB |
Output is correct |
37 |
Correct |
11 ms |
12416 KB |
Output is correct |
38 |
Correct |
7 ms |
12212 KB |
Output is correct |
39 |
Correct |
11 ms |
12416 KB |
Output is correct |
40 |
Correct |
9 ms |
12416 KB |
Output is correct |
41 |
Correct |
9 ms |
12416 KB |
Output is correct |
42 |
Correct |
9 ms |
12416 KB |
Output is correct |
43 |
Correct |
11 ms |
12544 KB |
Output is correct |
44 |
Correct |
8 ms |
12160 KB |
Output is correct |
45 |
Correct |
9 ms |
12416 KB |
Output is correct |
46 |
Correct |
13 ms |
12416 KB |
Output is correct |
47 |
Correct |
7 ms |
12160 KB |
Output is correct |
48 |
Correct |
9 ms |
12416 KB |
Output is correct |
49 |
Correct |
9 ms |
12416 KB |
Output is correct |
50 |
Correct |
9 ms |
12416 KB |
Output is correct |
51 |
Correct |
9 ms |
12416 KB |
Output is correct |
52 |
Correct |
9 ms |
12416 KB |
Output is correct |
53 |
Correct |
9 ms |
12416 KB |
Output is correct |
54 |
Correct |
9 ms |
12416 KB |
Output is correct |
55 |
Correct |
8 ms |
12160 KB |
Output is correct |
56 |
Correct |
73 ms |
21104 KB |
Output is correct |
57 |
Correct |
112 ms |
20976 KB |
Output is correct |
58 |
Correct |
9 ms |
12416 KB |
Output is correct |
59 |
Correct |
10 ms |
12160 KB |
Output is correct |
60 |
Correct |
8 ms |
12160 KB |
Output is correct |
61 |
Correct |
95 ms |
20848 KB |
Output is correct |
62 |
Correct |
9 ms |
12416 KB |
Output is correct |
63 |
Correct |
100 ms |
28016 KB |
Output is correct |
64 |
Correct |
98 ms |
20892 KB |
Output is correct |
65 |
Correct |
9 ms |
12416 KB |
Output is correct |
66 |
Correct |
96 ms |
21616 KB |
Output is correct |
67 |
Correct |
10 ms |
12416 KB |
Output is correct |
68 |
Correct |
9 ms |
12416 KB |
Output is correct |
69 |
Correct |
66 ms |
21100 KB |
Output is correct |
70 |
Correct |
13 ms |
12672 KB |
Output is correct |
71 |
Correct |
114 ms |
20848 KB |
Output is correct |
72 |
Correct |
138 ms |
23152 KB |
Output is correct |
73 |
Correct |
9 ms |
12192 KB |
Output is correct |
74 |
Correct |
100 ms |
28272 KB |
Output is correct |
75 |
Correct |
107 ms |
24560 KB |
Output is correct |
76 |
Correct |
9 ms |
12416 KB |
Output is correct |
77 |
Correct |
11 ms |
12416 KB |
Output is correct |
78 |
Correct |
69 ms |
21104 KB |
Output is correct |
79 |
Correct |
73 ms |
22768 KB |
Output is correct |
80 |
Correct |
10 ms |
12416 KB |
Output is correct |
81 |
Correct |
9 ms |
12416 KB |
Output is correct |
82 |
Correct |
10 ms |
12160 KB |
Output is correct |
83 |
Correct |
8 ms |
12160 KB |
Output is correct |
84 |
Correct |
9 ms |
12416 KB |
Output is correct |
85 |
Correct |
108 ms |
21872 KB |
Output is correct |
86 |
Correct |
9 ms |
12416 KB |
Output is correct |
87 |
Correct |
107 ms |
20976 KB |
Output is correct |
88 |
Correct |
8 ms |
12160 KB |
Output is correct |
89 |
Correct |
102 ms |
20976 KB |
Output is correct |
90 |
Correct |
109 ms |
21748 KB |
Output is correct |
91 |
Correct |
111 ms |
22508 KB |
Output is correct |
92 |
Correct |
84 ms |
21100 KB |
Output is correct |
93 |
Correct |
12 ms |
12416 KB |
Output is correct |
94 |
Correct |
8 ms |
12160 KB |
Output is correct |
95 |
Correct |
85 ms |
22636 KB |
Output is correct |
96 |
Correct |
151 ms |
26352 KB |
Output is correct |
97 |
Correct |
14 ms |
12416 KB |
Output is correct |
98 |
Correct |
8 ms |
12160 KB |
Output is correct |
99 |
Correct |
88 ms |
22000 KB |
Output is correct |
100 |
Correct |
13 ms |
12416 KB |
Output is correct |
101 |
Correct |
134 ms |
21100 KB |
Output is correct |
102 |
Correct |
132 ms |
25072 KB |
Output is correct |
103 |
Correct |
11 ms |
12416 KB |
Output is correct |
104 |
Correct |
11 ms |
12544 KB |
Output is correct |
105 |
Correct |
12 ms |
12416 KB |
Output is correct |
106 |
Correct |
11 ms |
12416 KB |
Output is correct |
107 |
Correct |
11 ms |
12416 KB |
Output is correct |
108 |
Correct |
572 ms |
62020 KB |
Output is correct |
109 |
Correct |
777 ms |
73700 KB |
Output is correct |
110 |
Correct |
781 ms |
84840 KB |
Output is correct |
111 |
Correct |
853 ms |
87524 KB |
Output is correct |
112 |
Correct |
940 ms |
77668 KB |
Output is correct |
113 |
Correct |
597 ms |
65220 KB |
Output is correct |
114 |
Correct |
740 ms |
55780 KB |
Output is correct |
115 |
Correct |
744 ms |
55692 KB |
Output is correct |
116 |
Correct |
872 ms |
58932 KB |
Output is correct |
117 |
Correct |
823 ms |
64484 KB |
Output is correct |
118 |
Correct |
712 ms |
55912 KB |
Output is correct |
119 |
Correct |
895 ms |
64356 KB |
Output is correct |
120 |
Correct |
907 ms |
78308 KB |
Output is correct |
121 |
Correct |
805 ms |
64352 KB |
Output is correct |
122 |
Correct |
877 ms |
61796 KB |
Output is correct |
123 |
Correct |
547 ms |
65888 KB |
Output is correct |
124 |
Correct |
824 ms |
63212 KB |
Output is correct |