//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
#define F first
#define S second
#define pb push_back
#define sze size()
#define all(x) x.begin() , x.end()
#define wall__ cout << "--------------------------------------\n";
#define kids int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);
typedef long long ll;
typedef long double dl;
typedef pair < int , int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;
const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = 2e16;
const ll INF = 1e9 + 10;
const ll lg = 32;
int n, k, a[N], timer, c[N], sub[N], ans, b[N];
int root, cnt[N], h[N], st[N], en[N], ord[N];
int sum;
vector < int > g[N];
bitset < N > mark;
void dfs0 (int v, int p) {
sub[v] = 1; st[v] = ++timer; ord[st[v]] = v;
h[v] = 0;
for (auto u : g[v]) {
if (p == u) continue;
dfs0(u, v);
if (sub[u] > sub[h[v]]) h[v] = u;
sub[v] += sub[u];
}
en[v] = timer;
}
void dfs (int v, int p) {
sum = 0;
for (auto u : g[v]) {
if (u == p || u == h[v]) continue;
dfs(u, v);
for (int i = st[u]; i <= en[u]; i++) mark[a[ord[i]]] = 0;
sum = 0;
cnt[v] += cnt[u];
}
if (h[v]) {
dfs(h[v], v); cnt[v] += cnt[h[v]];
for (int i = st[v]; i < st[h[v]]; i++) {
if (mark[a[ord[i]]] == 0) {
mark[a[ord[i]]] = 1;
sum += c[a[ord[i]]];
}
}
for (int i = en[h[v]] + 1; i <= en[v]; i++) {
if (mark[a[ord[i]]] == 0) {
mark[a[ord[i]]] = 1;
sum += c[a[ord[i]]];
}
}
}
if (mark[a[v]] == 0) {
mark[a[v]] = 1;
sum += c[a[v]];
}
if ((sum == sub[v] && v != root)) {
b[v] = 1;
++cnt[v];
}
}
void dfs1(int v, int p) {
if (b[v] == 1) {
if (cnt[v] == 1 || cnt[v] == cnt[root]) ++ans;
}
for (auto u : g[v]) {
if (u == p) continue;
dfs1(u, v);
}
}
void solve () {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int v, u; cin >> v >> u;
g[v].pb(u); g[u].pb(v);
}
for (int i = 1; i <= n; i++) {
cin >> a[i]; ++c[a[i]];
}
root = 1;
for (int i = 2; i <= n; i++) {
if (g[i].sze > g[root].sze) root = i;
}
dfs0(root, 0);
dfs(root, 0);
dfs1(root, 0);
cout << (ans + 1) / 2 << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {solve();}
return 0;
}
/*
*/
//shrek will AC this;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2632 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2680 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
1 ms |
2692 KB |
Output is correct |
20 |
Correct |
1 ms |
2684 KB |
Output is correct |
21 |
Correct |
2 ms |
2684 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2624 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2632 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2680 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
1 ms |
2692 KB |
Output is correct |
20 |
Correct |
1 ms |
2684 KB |
Output is correct |
21 |
Correct |
2 ms |
2684 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2624 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2952 KB |
Output is correct |
27 |
Correct |
2 ms |
2948 KB |
Output is correct |
28 |
Correct |
3 ms |
2948 KB |
Output is correct |
29 |
Correct |
3 ms |
2948 KB |
Output is correct |
30 |
Correct |
2 ms |
2900 KB |
Output is correct |
31 |
Correct |
1 ms |
2684 KB |
Output is correct |
32 |
Correct |
3 ms |
3160 KB |
Output is correct |
33 |
Correct |
1 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2820 KB |
Output is correct |
35 |
Correct |
2 ms |
2952 KB |
Output is correct |
36 |
Correct |
3 ms |
2916 KB |
Output is correct |
37 |
Correct |
2 ms |
2900 KB |
Output is correct |
38 |
Correct |
1 ms |
2644 KB |
Output is correct |
39 |
Correct |
3 ms |
2952 KB |
Output is correct |
40 |
Correct |
3 ms |
2824 KB |
Output is correct |
41 |
Correct |
2 ms |
2952 KB |
Output is correct |
42 |
Correct |
2 ms |
2884 KB |
Output is correct |
43 |
Correct |
3 ms |
3156 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2900 KB |
Output is correct |
46 |
Correct |
3 ms |
2900 KB |
Output is correct |
47 |
Correct |
2 ms |
2644 KB |
Output is correct |
48 |
Correct |
2 ms |
2900 KB |
Output is correct |
49 |
Correct |
2 ms |
2956 KB |
Output is correct |
50 |
Correct |
3 ms |
3028 KB |
Output is correct |
51 |
Correct |
3 ms |
2900 KB |
Output is correct |
52 |
Correct |
2 ms |
2900 KB |
Output is correct |
53 |
Correct |
3 ms |
2900 KB |
Output is correct |
54 |
Correct |
2 ms |
2832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2632 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2680 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
1 ms |
2692 KB |
Output is correct |
20 |
Correct |
1 ms |
2684 KB |
Output is correct |
21 |
Correct |
2 ms |
2684 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2624 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
29 ms |
9100 KB |
Output is correct |
27 |
Correct |
41 ms |
9164 KB |
Output is correct |
28 |
Correct |
2 ms |
2952 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
31 |
Correct |
42 ms |
9316 KB |
Output is correct |
32 |
Correct |
2 ms |
2900 KB |
Output is correct |
33 |
Correct |
51 ms |
17908 KB |
Output is correct |
34 |
Correct |
43 ms |
9220 KB |
Output is correct |
35 |
Correct |
3 ms |
2900 KB |
Output is correct |
36 |
Correct |
48 ms |
10188 KB |
Output is correct |
37 |
Correct |
2 ms |
2900 KB |
Output is correct |
38 |
Correct |
2 ms |
2900 KB |
Output is correct |
39 |
Correct |
28 ms |
9156 KB |
Output is correct |
40 |
Correct |
2 ms |
3156 KB |
Output is correct |
41 |
Correct |
45 ms |
9224 KB |
Output is correct |
42 |
Correct |
44 ms |
12376 KB |
Output is correct |
43 |
Correct |
1 ms |
2644 KB |
Output is correct |
44 |
Correct |
62 ms |
18304 KB |
Output is correct |
45 |
Correct |
42 ms |
13636 KB |
Output is correct |
46 |
Correct |
2 ms |
2900 KB |
Output is correct |
47 |
Correct |
2 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
8512 KB |
Output is correct |
2 |
Correct |
31 ms |
9788 KB |
Output is correct |
3 |
Correct |
3 ms |
2900 KB |
Output is correct |
4 |
Correct |
2 ms |
2948 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2932 KB |
Output is correct |
8 |
Correct |
46 ms |
10956 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
40 ms |
10568 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
43 ms |
10508 KB |
Output is correct |
13 |
Correct |
46 ms |
10828 KB |
Output is correct |
14 |
Correct |
41 ms |
11124 KB |
Output is correct |
15 |
Correct |
37 ms |
9896 KB |
Output is correct |
16 |
Correct |
2 ms |
2948 KB |
Output is correct |
17 |
Correct |
1 ms |
2704 KB |
Output is correct |
18 |
Correct |
30 ms |
11332 KB |
Output is correct |
19 |
Correct |
49 ms |
15880 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
1 ms |
2672 KB |
Output is correct |
22 |
Correct |
29 ms |
11084 KB |
Output is correct |
23 |
Correct |
2 ms |
2900 KB |
Output is correct |
24 |
Correct |
41 ms |
10516 KB |
Output is correct |
25 |
Correct |
59 ms |
14164 KB |
Output is correct |
26 |
Correct |
2 ms |
2900 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2900 KB |
Output is correct |
30 |
Correct |
2 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2632 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2680 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
1 ms |
2692 KB |
Output is correct |
20 |
Correct |
1 ms |
2684 KB |
Output is correct |
21 |
Correct |
2 ms |
2684 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2624 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2952 KB |
Output is correct |
27 |
Correct |
2 ms |
2948 KB |
Output is correct |
28 |
Correct |
3 ms |
2948 KB |
Output is correct |
29 |
Correct |
3 ms |
2948 KB |
Output is correct |
30 |
Correct |
2 ms |
2900 KB |
Output is correct |
31 |
Correct |
1 ms |
2684 KB |
Output is correct |
32 |
Correct |
3 ms |
3160 KB |
Output is correct |
33 |
Correct |
1 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2820 KB |
Output is correct |
35 |
Correct |
2 ms |
2952 KB |
Output is correct |
36 |
Correct |
3 ms |
2916 KB |
Output is correct |
37 |
Correct |
2 ms |
2900 KB |
Output is correct |
38 |
Correct |
1 ms |
2644 KB |
Output is correct |
39 |
Correct |
3 ms |
2952 KB |
Output is correct |
40 |
Correct |
3 ms |
2824 KB |
Output is correct |
41 |
Correct |
2 ms |
2952 KB |
Output is correct |
42 |
Correct |
2 ms |
2884 KB |
Output is correct |
43 |
Correct |
3 ms |
3156 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2900 KB |
Output is correct |
46 |
Correct |
3 ms |
2900 KB |
Output is correct |
47 |
Correct |
2 ms |
2644 KB |
Output is correct |
48 |
Correct |
2 ms |
2900 KB |
Output is correct |
49 |
Correct |
2 ms |
2956 KB |
Output is correct |
50 |
Correct |
3 ms |
3028 KB |
Output is correct |
51 |
Correct |
3 ms |
2900 KB |
Output is correct |
52 |
Correct |
2 ms |
2900 KB |
Output is correct |
53 |
Correct |
3 ms |
2900 KB |
Output is correct |
54 |
Correct |
2 ms |
2832 KB |
Output is correct |
55 |
Correct |
1 ms |
2644 KB |
Output is correct |
56 |
Correct |
29 ms |
9100 KB |
Output is correct |
57 |
Correct |
41 ms |
9164 KB |
Output is correct |
58 |
Correct |
2 ms |
2952 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
1 ms |
2644 KB |
Output is correct |
61 |
Correct |
42 ms |
9316 KB |
Output is correct |
62 |
Correct |
2 ms |
2900 KB |
Output is correct |
63 |
Correct |
51 ms |
17908 KB |
Output is correct |
64 |
Correct |
43 ms |
9220 KB |
Output is correct |
65 |
Correct |
3 ms |
2900 KB |
Output is correct |
66 |
Correct |
48 ms |
10188 KB |
Output is correct |
67 |
Correct |
2 ms |
2900 KB |
Output is correct |
68 |
Correct |
2 ms |
2900 KB |
Output is correct |
69 |
Correct |
28 ms |
9156 KB |
Output is correct |
70 |
Correct |
2 ms |
3156 KB |
Output is correct |
71 |
Correct |
45 ms |
9224 KB |
Output is correct |
72 |
Correct |
44 ms |
12376 KB |
Output is correct |
73 |
Correct |
1 ms |
2644 KB |
Output is correct |
74 |
Correct |
62 ms |
18304 KB |
Output is correct |
75 |
Correct |
42 ms |
13636 KB |
Output is correct |
76 |
Correct |
2 ms |
2900 KB |
Output is correct |
77 |
Correct |
2 ms |
2900 KB |
Output is correct |
78 |
Correct |
27 ms |
8512 KB |
Output is correct |
79 |
Correct |
31 ms |
9788 KB |
Output is correct |
80 |
Correct |
3 ms |
2900 KB |
Output is correct |
81 |
Correct |
2 ms |
2948 KB |
Output is correct |
82 |
Correct |
1 ms |
2644 KB |
Output is correct |
83 |
Correct |
1 ms |
2688 KB |
Output is correct |
84 |
Correct |
2 ms |
2932 KB |
Output is correct |
85 |
Correct |
46 ms |
10956 KB |
Output is correct |
86 |
Correct |
2 ms |
2900 KB |
Output is correct |
87 |
Correct |
40 ms |
10568 KB |
Output is correct |
88 |
Correct |
1 ms |
2644 KB |
Output is correct |
89 |
Correct |
43 ms |
10508 KB |
Output is correct |
90 |
Correct |
46 ms |
10828 KB |
Output is correct |
91 |
Correct |
41 ms |
11124 KB |
Output is correct |
92 |
Correct |
37 ms |
9896 KB |
Output is correct |
93 |
Correct |
2 ms |
2948 KB |
Output is correct |
94 |
Correct |
1 ms |
2704 KB |
Output is correct |
95 |
Correct |
30 ms |
11332 KB |
Output is correct |
96 |
Correct |
49 ms |
15880 KB |
Output is correct |
97 |
Correct |
2 ms |
2900 KB |
Output is correct |
98 |
Correct |
1 ms |
2672 KB |
Output is correct |
99 |
Correct |
29 ms |
11084 KB |
Output is correct |
100 |
Correct |
2 ms |
2900 KB |
Output is correct |
101 |
Correct |
41 ms |
10516 KB |
Output is correct |
102 |
Correct |
59 ms |
14164 KB |
Output is correct |
103 |
Correct |
2 ms |
2900 KB |
Output is correct |
104 |
Correct |
2 ms |
3028 KB |
Output is correct |
105 |
Correct |
2 ms |
2900 KB |
Output is correct |
106 |
Correct |
2 ms |
2900 KB |
Output is correct |
107 |
Correct |
2 ms |
2900 KB |
Output is correct |
108 |
Runtime error |
4 ms |
5204 KB |
Execution killed with signal 11 |
109 |
Halted |
0 ms |
0 KB |
- |