#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 ds[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);
if (a != b) {
if (ds[a] > ds[b]) swap(a, b);
par[a] = b;
ds[b] += ds[a];
}
}
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;
ds[i] = 1;
}
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 |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
10 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
7 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
7 ms |
12160 KB |
Output is correct |
11 |
Correct |
7 ms |
12160 KB |
Output is correct |
12 |
Correct |
7 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
7 ms |
12160 KB |
Output is correct |
15 |
Correct |
7 ms |
12160 KB |
Output is correct |
16 |
Correct |
7 ms |
12160 KB |
Output is correct |
17 |
Correct |
7 ms |
12160 KB |
Output is correct |
18 |
Correct |
7 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 |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
7 ms |
12160 KB |
Output is correct |
23 |
Correct |
7 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
10 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
7 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
7 ms |
12160 KB |
Output is correct |
11 |
Correct |
7 ms |
12160 KB |
Output is correct |
12 |
Correct |
7 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
7 ms |
12160 KB |
Output is correct |
15 |
Correct |
7 ms |
12160 KB |
Output is correct |
16 |
Correct |
7 ms |
12160 KB |
Output is correct |
17 |
Correct |
7 ms |
12160 KB |
Output is correct |
18 |
Correct |
7 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 |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
7 ms |
12160 KB |
Output is correct |
23 |
Correct |
7 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
9 ms |
12160 KB |
Output is correct |
26 |
Correct |
9 ms |
12416 KB |
Output is correct |
27 |
Correct |
9 ms |
12416 KB |
Output is correct |
28 |
Correct |
10 ms |
12544 KB |
Output is correct |
29 |
Correct |
11 ms |
12416 KB |
Output is correct |
30 |
Correct |
11 ms |
12416 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
9 ms |
12544 KB |
Output is correct |
33 |
Correct |
7 ms |
12160 KB |
Output is correct |
34 |
Correct |
9 ms |
12416 KB |
Output is correct |
35 |
Correct |
9 ms |
12416 KB |
Output is correct |
36 |
Correct |
11 ms |
12416 KB |
Output is correct |
37 |
Correct |
11 ms |
12416 KB |
Output is correct |
38 |
Correct |
7 ms |
12160 KB |
Output is correct |
39 |
Correct |
11 ms |
12416 KB |
Output is correct |
40 |
Correct |
11 ms |
12416 KB |
Output is correct |
41 |
Correct |
11 ms |
12416 KB |
Output is correct |
42 |
Correct |
9 ms |
12416 KB |
Output is correct |
43 |
Correct |
11 ms |
12680 KB |
Output is correct |
44 |
Correct |
8 ms |
12160 KB |
Output is correct |
45 |
Correct |
9 ms |
12544 KB |
Output is correct |
46 |
Correct |
9 ms |
12416 KB |
Output is correct |
47 |
Correct |
8 ms |
12160 KB |
Output is correct |
48 |
Correct |
9 ms |
12416 KB |
Output is correct |
49 |
Correct |
11 ms |
12416 KB |
Output is correct |
50 |
Correct |
9 ms |
12544 KB |
Output is correct |
51 |
Correct |
10 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 |
12 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
10 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
7 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
7 ms |
12160 KB |
Output is correct |
11 |
Correct |
7 ms |
12160 KB |
Output is correct |
12 |
Correct |
7 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
7 ms |
12160 KB |
Output is correct |
15 |
Correct |
7 ms |
12160 KB |
Output is correct |
16 |
Correct |
7 ms |
12160 KB |
Output is correct |
17 |
Correct |
7 ms |
12160 KB |
Output is correct |
18 |
Correct |
7 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 |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
7 ms |
12160 KB |
Output is correct |
23 |
Correct |
7 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
85 ms |
20208 KB |
Output is correct |
27 |
Correct |
134 ms |
19952 KB |
Output is correct |
28 |
Correct |
12 ms |
12416 KB |
Output is correct |
29 |
Correct |
10 ms |
12160 KB |
Output is correct |
30 |
Correct |
9 ms |
12160 KB |
Output is correct |
31 |
Correct |
98 ms |
20136 KB |
Output is correct |
32 |
Correct |
11 ms |
12416 KB |
Output is correct |
33 |
Correct |
103 ms |
27248 KB |
Output is correct |
34 |
Correct |
96 ms |
19948 KB |
Output is correct |
35 |
Correct |
9 ms |
12416 KB |
Output is correct |
36 |
Correct |
125 ms |
20720 KB |
Output is correct |
37 |
Correct |
9 ms |
12416 KB |
Output is correct |
38 |
Correct |
11 ms |
12416 KB |
Output is correct |
39 |
Correct |
69 ms |
20204 KB |
Output is correct |
40 |
Correct |
10 ms |
12672 KB |
Output is correct |
41 |
Correct |
123 ms |
20080 KB |
Output is correct |
42 |
Correct |
126 ms |
22256 KB |
Output is correct |
43 |
Correct |
8 ms |
12160 KB |
Output is correct |
44 |
Correct |
116 ms |
27376 KB |
Output is correct |
45 |
Correct |
124 ms |
23664 KB |
Output is correct |
46 |
Correct |
9 ms |
12416 KB |
Output is correct |
47 |
Correct |
11 ms |
12544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
20208 KB |
Output is correct |
2 |
Correct |
75 ms |
21488 KB |
Output is correct |
3 |
Correct |
9 ms |
12416 KB |
Output is correct |
4 |
Correct |
9 ms |
12416 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12416 KB |
Output is correct |
8 |
Correct |
124 ms |
20592 KB |
Output is correct |
9 |
Correct |
11 ms |
12416 KB |
Output is correct |
10 |
Correct |
116 ms |
19952 KB |
Output is correct |
11 |
Correct |
9 ms |
12160 KB |
Output is correct |
12 |
Correct |
93 ms |
19952 KB |
Output is correct |
13 |
Correct |
112 ms |
20604 KB |
Output is correct |
14 |
Correct |
134 ms |
21232 KB |
Output is correct |
15 |
Correct |
63 ms |
20204 KB |
Output is correct |
16 |
Correct |
9 ms |
12544 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
101 ms |
21484 KB |
Output is correct |
19 |
Correct |
135 ms |
25144 KB |
Output is correct |
20 |
Correct |
9 ms |
12416 KB |
Output is correct |
21 |
Correct |
9 ms |
12160 KB |
Output is correct |
22 |
Correct |
73 ms |
20852 KB |
Output is correct |
23 |
Correct |
9 ms |
12416 KB |
Output is correct |
24 |
Correct |
127 ms |
19952 KB |
Output is correct |
25 |
Correct |
121 ms |
23920 KB |
Output is correct |
26 |
Correct |
10 ms |
12544 KB |
Output is correct |
27 |
Correct |
9 ms |
12544 KB |
Output is correct |
28 |
Correct |
9 ms |
12416 KB |
Output is correct |
29 |
Correct |
9 ms |
12416 KB |
Output is correct |
30 |
Correct |
10 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
10 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
7 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
7 ms |
12160 KB |
Output is correct |
11 |
Correct |
7 ms |
12160 KB |
Output is correct |
12 |
Correct |
7 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
7 ms |
12160 KB |
Output is correct |
15 |
Correct |
7 ms |
12160 KB |
Output is correct |
16 |
Correct |
7 ms |
12160 KB |
Output is correct |
17 |
Correct |
7 ms |
12160 KB |
Output is correct |
18 |
Correct |
7 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 |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
7 ms |
12160 KB |
Output is correct |
23 |
Correct |
7 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
9 ms |
12160 KB |
Output is correct |
26 |
Correct |
9 ms |
12416 KB |
Output is correct |
27 |
Correct |
9 ms |
12416 KB |
Output is correct |
28 |
Correct |
10 ms |
12544 KB |
Output is correct |
29 |
Correct |
11 ms |
12416 KB |
Output is correct |
30 |
Correct |
11 ms |
12416 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
9 ms |
12544 KB |
Output is correct |
33 |
Correct |
7 ms |
12160 KB |
Output is correct |
34 |
Correct |
9 ms |
12416 KB |
Output is correct |
35 |
Correct |
9 ms |
12416 KB |
Output is correct |
36 |
Correct |
11 ms |
12416 KB |
Output is correct |
37 |
Correct |
11 ms |
12416 KB |
Output is correct |
38 |
Correct |
7 ms |
12160 KB |
Output is correct |
39 |
Correct |
11 ms |
12416 KB |
Output is correct |
40 |
Correct |
11 ms |
12416 KB |
Output is correct |
41 |
Correct |
11 ms |
12416 KB |
Output is correct |
42 |
Correct |
9 ms |
12416 KB |
Output is correct |
43 |
Correct |
11 ms |
12680 KB |
Output is correct |
44 |
Correct |
8 ms |
12160 KB |
Output is correct |
45 |
Correct |
9 ms |
12544 KB |
Output is correct |
46 |
Correct |
9 ms |
12416 KB |
Output is correct |
47 |
Correct |
8 ms |
12160 KB |
Output is correct |
48 |
Correct |
9 ms |
12416 KB |
Output is correct |
49 |
Correct |
11 ms |
12416 KB |
Output is correct |
50 |
Correct |
9 ms |
12544 KB |
Output is correct |
51 |
Correct |
10 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 |
12 ms |
12416 KB |
Output is correct |
55 |
Correct |
8 ms |
12160 KB |
Output is correct |
56 |
Correct |
85 ms |
20208 KB |
Output is correct |
57 |
Correct |
134 ms |
19952 KB |
Output is correct |
58 |
Correct |
12 ms |
12416 KB |
Output is correct |
59 |
Correct |
10 ms |
12160 KB |
Output is correct |
60 |
Correct |
9 ms |
12160 KB |
Output is correct |
61 |
Correct |
98 ms |
20136 KB |
Output is correct |
62 |
Correct |
11 ms |
12416 KB |
Output is correct |
63 |
Correct |
103 ms |
27248 KB |
Output is correct |
64 |
Correct |
96 ms |
19948 KB |
Output is correct |
65 |
Correct |
9 ms |
12416 KB |
Output is correct |
66 |
Correct |
125 ms |
20720 KB |
Output is correct |
67 |
Correct |
9 ms |
12416 KB |
Output is correct |
68 |
Correct |
11 ms |
12416 KB |
Output is correct |
69 |
Correct |
69 ms |
20204 KB |
Output is correct |
70 |
Correct |
10 ms |
12672 KB |
Output is correct |
71 |
Correct |
123 ms |
20080 KB |
Output is correct |
72 |
Correct |
126 ms |
22256 KB |
Output is correct |
73 |
Correct |
8 ms |
12160 KB |
Output is correct |
74 |
Correct |
116 ms |
27376 KB |
Output is correct |
75 |
Correct |
124 ms |
23664 KB |
Output is correct |
76 |
Correct |
9 ms |
12416 KB |
Output is correct |
77 |
Correct |
11 ms |
12544 KB |
Output is correct |
78 |
Correct |
62 ms |
20208 KB |
Output is correct |
79 |
Correct |
75 ms |
21488 KB |
Output is correct |
80 |
Correct |
9 ms |
12416 KB |
Output is correct |
81 |
Correct |
9 ms |
12416 KB |
Output is correct |
82 |
Correct |
8 ms |
12160 KB |
Output is correct |
83 |
Correct |
9 ms |
12160 KB |
Output is correct |
84 |
Correct |
9 ms |
12416 KB |
Output is correct |
85 |
Correct |
124 ms |
20592 KB |
Output is correct |
86 |
Correct |
11 ms |
12416 KB |
Output is correct |
87 |
Correct |
116 ms |
19952 KB |
Output is correct |
88 |
Correct |
9 ms |
12160 KB |
Output is correct |
89 |
Correct |
93 ms |
19952 KB |
Output is correct |
90 |
Correct |
112 ms |
20604 KB |
Output is correct |
91 |
Correct |
134 ms |
21232 KB |
Output is correct |
92 |
Correct |
63 ms |
20204 KB |
Output is correct |
93 |
Correct |
9 ms |
12544 KB |
Output is correct |
94 |
Correct |
8 ms |
12160 KB |
Output is correct |
95 |
Correct |
101 ms |
21484 KB |
Output is correct |
96 |
Correct |
135 ms |
25144 KB |
Output is correct |
97 |
Correct |
9 ms |
12416 KB |
Output is correct |
98 |
Correct |
9 ms |
12160 KB |
Output is correct |
99 |
Correct |
73 ms |
20852 KB |
Output is correct |
100 |
Correct |
9 ms |
12416 KB |
Output is correct |
101 |
Correct |
127 ms |
19952 KB |
Output is correct |
102 |
Correct |
121 ms |
23920 KB |
Output is correct |
103 |
Correct |
10 ms |
12544 KB |
Output is correct |
104 |
Correct |
9 ms |
12544 KB |
Output is correct |
105 |
Correct |
9 ms |
12416 KB |
Output is correct |
106 |
Correct |
9 ms |
12416 KB |
Output is correct |
107 |
Correct |
10 ms |
12416 KB |
Output is correct |
108 |
Correct |
609 ms |
54260 KB |
Output is correct |
109 |
Correct |
994 ms |
68068 KB |
Output is correct |
110 |
Correct |
1022 ms |
79436 KB |
Output is correct |
111 |
Correct |
1008 ms |
79460 KB |
Output is correct |
112 |
Correct |
1196 ms |
69732 KB |
Output is correct |
113 |
Correct |
787 ms |
57172 KB |
Output is correct |
114 |
Correct |
958 ms |
50148 KB |
Output is correct |
115 |
Correct |
948 ms |
50148 KB |
Output is correct |
116 |
Correct |
1087 ms |
51428 KB |
Output is correct |
117 |
Correct |
823 ms |
56500 KB |
Output is correct |
118 |
Correct |
823 ms |
49512 KB |
Output is correct |
119 |
Correct |
1008 ms |
56548 KB |
Output is correct |
120 |
Correct |
893 ms |
70588 KB |
Output is correct |
121 |
Correct |
946 ms |
56560 KB |
Output is correct |
122 |
Correct |
844 ms |
54244 KB |
Output is correct |
123 |
Correct |
553 ms |
58080 KB |
Output is correct |
124 |
Correct |
1038 ms |
55528 KB |
Output is correct |