//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 = 5e5 + 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 |
5 ms |
12116 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12072 KB |
Output is correct |
4 |
Correct |
5 ms |
12120 KB |
Output is correct |
5 |
Correct |
5 ms |
12136 KB |
Output is correct |
6 |
Correct |
5 ms |
12116 KB |
Output is correct |
7 |
Correct |
5 ms |
12028 KB |
Output is correct |
8 |
Correct |
5 ms |
12116 KB |
Output is correct |
9 |
Correct |
5 ms |
12116 KB |
Output is correct |
10 |
Correct |
5 ms |
12116 KB |
Output is correct |
11 |
Correct |
5 ms |
12116 KB |
Output is correct |
12 |
Correct |
5 ms |
12116 KB |
Output is correct |
13 |
Correct |
5 ms |
12116 KB |
Output is correct |
14 |
Correct |
5 ms |
12024 KB |
Output is correct |
15 |
Correct |
5 ms |
12116 KB |
Output is correct |
16 |
Correct |
7 ms |
12116 KB |
Output is correct |
17 |
Correct |
6 ms |
12100 KB |
Output is correct |
18 |
Correct |
5 ms |
12116 KB |
Output is correct |
19 |
Correct |
5 ms |
12116 KB |
Output is correct |
20 |
Correct |
5 ms |
12048 KB |
Output is correct |
21 |
Correct |
5 ms |
12116 KB |
Output is correct |
22 |
Correct |
6 ms |
12116 KB |
Output is correct |
23 |
Correct |
5 ms |
12116 KB |
Output is correct |
24 |
Correct |
5 ms |
12136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12116 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12072 KB |
Output is correct |
4 |
Correct |
5 ms |
12120 KB |
Output is correct |
5 |
Correct |
5 ms |
12136 KB |
Output is correct |
6 |
Correct |
5 ms |
12116 KB |
Output is correct |
7 |
Correct |
5 ms |
12028 KB |
Output is correct |
8 |
Correct |
5 ms |
12116 KB |
Output is correct |
9 |
Correct |
5 ms |
12116 KB |
Output is correct |
10 |
Correct |
5 ms |
12116 KB |
Output is correct |
11 |
Correct |
5 ms |
12116 KB |
Output is correct |
12 |
Correct |
5 ms |
12116 KB |
Output is correct |
13 |
Correct |
5 ms |
12116 KB |
Output is correct |
14 |
Correct |
5 ms |
12024 KB |
Output is correct |
15 |
Correct |
5 ms |
12116 KB |
Output is correct |
16 |
Correct |
7 ms |
12116 KB |
Output is correct |
17 |
Correct |
6 ms |
12100 KB |
Output is correct |
18 |
Correct |
5 ms |
12116 KB |
Output is correct |
19 |
Correct |
5 ms |
12116 KB |
Output is correct |
20 |
Correct |
5 ms |
12048 KB |
Output is correct |
21 |
Correct |
5 ms |
12116 KB |
Output is correct |
22 |
Correct |
6 ms |
12116 KB |
Output is correct |
23 |
Correct |
5 ms |
12116 KB |
Output is correct |
24 |
Correct |
5 ms |
12136 KB |
Output is correct |
25 |
Correct |
5 ms |
12140 KB |
Output is correct |
26 |
Correct |
6 ms |
12244 KB |
Output is correct |
27 |
Correct |
6 ms |
12244 KB |
Output is correct |
28 |
Correct |
6 ms |
12372 KB |
Output is correct |
29 |
Correct |
6 ms |
12244 KB |
Output is correct |
30 |
Correct |
6 ms |
12244 KB |
Output is correct |
31 |
Correct |
6 ms |
12040 KB |
Output is correct |
32 |
Correct |
7 ms |
12416 KB |
Output is correct |
33 |
Correct |
5 ms |
12064 KB |
Output is correct |
34 |
Correct |
6 ms |
12244 KB |
Output is correct |
35 |
Correct |
8 ms |
12244 KB |
Output is correct |
36 |
Correct |
6 ms |
12244 KB |
Output is correct |
37 |
Correct |
6 ms |
12320 KB |
Output is correct |
38 |
Correct |
5 ms |
12116 KB |
Output is correct |
39 |
Correct |
6 ms |
12244 KB |
Output is correct |
40 |
Correct |
6 ms |
12244 KB |
Output is correct |
41 |
Correct |
6 ms |
12328 KB |
Output is correct |
42 |
Correct |
6 ms |
12244 KB |
Output is correct |
43 |
Correct |
7 ms |
12500 KB |
Output is correct |
44 |
Correct |
5 ms |
12116 KB |
Output is correct |
45 |
Correct |
6 ms |
12372 KB |
Output is correct |
46 |
Correct |
6 ms |
12340 KB |
Output is correct |
47 |
Correct |
5 ms |
12116 KB |
Output is correct |
48 |
Correct |
6 ms |
12244 KB |
Output is correct |
49 |
Correct |
6 ms |
12244 KB |
Output is correct |
50 |
Correct |
6 ms |
12372 KB |
Output is correct |
51 |
Correct |
6 ms |
12244 KB |
Output is correct |
52 |
Correct |
6 ms |
12244 KB |
Output is correct |
53 |
Correct |
6 ms |
12244 KB |
Output is correct |
54 |
Correct |
7 ms |
12308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12116 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12072 KB |
Output is correct |
4 |
Correct |
5 ms |
12120 KB |
Output is correct |
5 |
Correct |
5 ms |
12136 KB |
Output is correct |
6 |
Correct |
5 ms |
12116 KB |
Output is correct |
7 |
Correct |
5 ms |
12028 KB |
Output is correct |
8 |
Correct |
5 ms |
12116 KB |
Output is correct |
9 |
Correct |
5 ms |
12116 KB |
Output is correct |
10 |
Correct |
5 ms |
12116 KB |
Output is correct |
11 |
Correct |
5 ms |
12116 KB |
Output is correct |
12 |
Correct |
5 ms |
12116 KB |
Output is correct |
13 |
Correct |
5 ms |
12116 KB |
Output is correct |
14 |
Correct |
5 ms |
12024 KB |
Output is correct |
15 |
Correct |
5 ms |
12116 KB |
Output is correct |
16 |
Correct |
7 ms |
12116 KB |
Output is correct |
17 |
Correct |
6 ms |
12100 KB |
Output is correct |
18 |
Correct |
5 ms |
12116 KB |
Output is correct |
19 |
Correct |
5 ms |
12116 KB |
Output is correct |
20 |
Correct |
5 ms |
12048 KB |
Output is correct |
21 |
Correct |
5 ms |
12116 KB |
Output is correct |
22 |
Correct |
6 ms |
12116 KB |
Output is correct |
23 |
Correct |
5 ms |
12116 KB |
Output is correct |
24 |
Correct |
5 ms |
12136 KB |
Output is correct |
25 |
Correct |
5 ms |
12092 KB |
Output is correct |
26 |
Correct |
32 ms |
17936 KB |
Output is correct |
27 |
Correct |
50 ms |
18100 KB |
Output is correct |
28 |
Correct |
7 ms |
12244 KB |
Output is correct |
29 |
Correct |
6 ms |
12056 KB |
Output is correct |
30 |
Correct |
5 ms |
12116 KB |
Output is correct |
31 |
Correct |
44 ms |
18240 KB |
Output is correct |
32 |
Correct |
6 ms |
12244 KB |
Output is correct |
33 |
Correct |
52 ms |
26676 KB |
Output is correct |
34 |
Correct |
45 ms |
18004 KB |
Output is correct |
35 |
Correct |
6 ms |
12260 KB |
Output is correct |
36 |
Correct |
46 ms |
19052 KB |
Output is correct |
37 |
Correct |
6 ms |
12244 KB |
Output is correct |
38 |
Correct |
6 ms |
12244 KB |
Output is correct |
39 |
Correct |
35 ms |
17952 KB |
Output is correct |
40 |
Correct |
6 ms |
12500 KB |
Output is correct |
41 |
Correct |
43 ms |
18036 KB |
Output is correct |
42 |
Correct |
46 ms |
21188 KB |
Output is correct |
43 |
Correct |
5 ms |
12116 KB |
Output is correct |
44 |
Correct |
52 ms |
27072 KB |
Output is correct |
45 |
Correct |
47 ms |
22576 KB |
Output is correct |
46 |
Correct |
6 ms |
12244 KB |
Output is correct |
47 |
Correct |
7 ms |
12224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
17928 KB |
Output is correct |
2 |
Correct |
35 ms |
19168 KB |
Output is correct |
3 |
Correct |
7 ms |
12272 KB |
Output is correct |
4 |
Correct |
6 ms |
12244 KB |
Output is correct |
5 |
Correct |
6 ms |
12020 KB |
Output is correct |
6 |
Correct |
6 ms |
12020 KB |
Output is correct |
7 |
Correct |
6 ms |
12244 KB |
Output is correct |
8 |
Correct |
48 ms |
18636 KB |
Output is correct |
9 |
Correct |
6 ms |
12244 KB |
Output is correct |
10 |
Correct |
45 ms |
18408 KB |
Output is correct |
11 |
Correct |
6 ms |
12036 KB |
Output is correct |
12 |
Correct |
45 ms |
18424 KB |
Output is correct |
13 |
Correct |
50 ms |
18552 KB |
Output is correct |
14 |
Correct |
47 ms |
18892 KB |
Output is correct |
15 |
Correct |
33 ms |
17932 KB |
Output is correct |
16 |
Correct |
7 ms |
12244 KB |
Output is correct |
17 |
Correct |
5 ms |
12116 KB |
Output is correct |
18 |
Correct |
36 ms |
19060 KB |
Output is correct |
19 |
Correct |
51 ms |
23608 KB |
Output is correct |
20 |
Correct |
6 ms |
12340 KB |
Output is correct |
21 |
Correct |
5 ms |
12116 KB |
Output is correct |
22 |
Correct |
35 ms |
18968 KB |
Output is correct |
23 |
Correct |
6 ms |
12332 KB |
Output is correct |
24 |
Correct |
47 ms |
18380 KB |
Output is correct |
25 |
Correct |
49 ms |
21908 KB |
Output is correct |
26 |
Correct |
6 ms |
12244 KB |
Output is correct |
27 |
Correct |
7 ms |
12312 KB |
Output is correct |
28 |
Correct |
7 ms |
12244 KB |
Output is correct |
29 |
Correct |
6 ms |
12244 KB |
Output is correct |
30 |
Correct |
6 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12116 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12072 KB |
Output is correct |
4 |
Correct |
5 ms |
12120 KB |
Output is correct |
5 |
Correct |
5 ms |
12136 KB |
Output is correct |
6 |
Correct |
5 ms |
12116 KB |
Output is correct |
7 |
Correct |
5 ms |
12028 KB |
Output is correct |
8 |
Correct |
5 ms |
12116 KB |
Output is correct |
9 |
Correct |
5 ms |
12116 KB |
Output is correct |
10 |
Correct |
5 ms |
12116 KB |
Output is correct |
11 |
Correct |
5 ms |
12116 KB |
Output is correct |
12 |
Correct |
5 ms |
12116 KB |
Output is correct |
13 |
Correct |
5 ms |
12116 KB |
Output is correct |
14 |
Correct |
5 ms |
12024 KB |
Output is correct |
15 |
Correct |
5 ms |
12116 KB |
Output is correct |
16 |
Correct |
7 ms |
12116 KB |
Output is correct |
17 |
Correct |
6 ms |
12100 KB |
Output is correct |
18 |
Correct |
5 ms |
12116 KB |
Output is correct |
19 |
Correct |
5 ms |
12116 KB |
Output is correct |
20 |
Correct |
5 ms |
12048 KB |
Output is correct |
21 |
Correct |
5 ms |
12116 KB |
Output is correct |
22 |
Correct |
6 ms |
12116 KB |
Output is correct |
23 |
Correct |
5 ms |
12116 KB |
Output is correct |
24 |
Correct |
5 ms |
12136 KB |
Output is correct |
25 |
Correct |
5 ms |
12140 KB |
Output is correct |
26 |
Correct |
6 ms |
12244 KB |
Output is correct |
27 |
Correct |
6 ms |
12244 KB |
Output is correct |
28 |
Correct |
6 ms |
12372 KB |
Output is correct |
29 |
Correct |
6 ms |
12244 KB |
Output is correct |
30 |
Correct |
6 ms |
12244 KB |
Output is correct |
31 |
Correct |
6 ms |
12040 KB |
Output is correct |
32 |
Correct |
7 ms |
12416 KB |
Output is correct |
33 |
Correct |
5 ms |
12064 KB |
Output is correct |
34 |
Correct |
6 ms |
12244 KB |
Output is correct |
35 |
Correct |
8 ms |
12244 KB |
Output is correct |
36 |
Correct |
6 ms |
12244 KB |
Output is correct |
37 |
Correct |
6 ms |
12320 KB |
Output is correct |
38 |
Correct |
5 ms |
12116 KB |
Output is correct |
39 |
Correct |
6 ms |
12244 KB |
Output is correct |
40 |
Correct |
6 ms |
12244 KB |
Output is correct |
41 |
Correct |
6 ms |
12328 KB |
Output is correct |
42 |
Correct |
6 ms |
12244 KB |
Output is correct |
43 |
Correct |
7 ms |
12500 KB |
Output is correct |
44 |
Correct |
5 ms |
12116 KB |
Output is correct |
45 |
Correct |
6 ms |
12372 KB |
Output is correct |
46 |
Correct |
6 ms |
12340 KB |
Output is correct |
47 |
Correct |
5 ms |
12116 KB |
Output is correct |
48 |
Correct |
6 ms |
12244 KB |
Output is correct |
49 |
Correct |
6 ms |
12244 KB |
Output is correct |
50 |
Correct |
6 ms |
12372 KB |
Output is correct |
51 |
Correct |
6 ms |
12244 KB |
Output is correct |
52 |
Correct |
6 ms |
12244 KB |
Output is correct |
53 |
Correct |
6 ms |
12244 KB |
Output is correct |
54 |
Correct |
7 ms |
12308 KB |
Output is correct |
55 |
Correct |
5 ms |
12092 KB |
Output is correct |
56 |
Correct |
32 ms |
17936 KB |
Output is correct |
57 |
Correct |
50 ms |
18100 KB |
Output is correct |
58 |
Correct |
7 ms |
12244 KB |
Output is correct |
59 |
Correct |
6 ms |
12056 KB |
Output is correct |
60 |
Correct |
5 ms |
12116 KB |
Output is correct |
61 |
Correct |
44 ms |
18240 KB |
Output is correct |
62 |
Correct |
6 ms |
12244 KB |
Output is correct |
63 |
Correct |
52 ms |
26676 KB |
Output is correct |
64 |
Correct |
45 ms |
18004 KB |
Output is correct |
65 |
Correct |
6 ms |
12260 KB |
Output is correct |
66 |
Correct |
46 ms |
19052 KB |
Output is correct |
67 |
Correct |
6 ms |
12244 KB |
Output is correct |
68 |
Correct |
6 ms |
12244 KB |
Output is correct |
69 |
Correct |
35 ms |
17952 KB |
Output is correct |
70 |
Correct |
6 ms |
12500 KB |
Output is correct |
71 |
Correct |
43 ms |
18036 KB |
Output is correct |
72 |
Correct |
46 ms |
21188 KB |
Output is correct |
73 |
Correct |
5 ms |
12116 KB |
Output is correct |
74 |
Correct |
52 ms |
27072 KB |
Output is correct |
75 |
Correct |
47 ms |
22576 KB |
Output is correct |
76 |
Correct |
6 ms |
12244 KB |
Output is correct |
77 |
Correct |
7 ms |
12224 KB |
Output is correct |
78 |
Correct |
33 ms |
17928 KB |
Output is correct |
79 |
Correct |
35 ms |
19168 KB |
Output is correct |
80 |
Correct |
7 ms |
12272 KB |
Output is correct |
81 |
Correct |
6 ms |
12244 KB |
Output is correct |
82 |
Correct |
6 ms |
12020 KB |
Output is correct |
83 |
Correct |
6 ms |
12020 KB |
Output is correct |
84 |
Correct |
6 ms |
12244 KB |
Output is correct |
85 |
Correct |
48 ms |
18636 KB |
Output is correct |
86 |
Correct |
6 ms |
12244 KB |
Output is correct |
87 |
Correct |
45 ms |
18408 KB |
Output is correct |
88 |
Correct |
6 ms |
12036 KB |
Output is correct |
89 |
Correct |
45 ms |
18424 KB |
Output is correct |
90 |
Correct |
50 ms |
18552 KB |
Output is correct |
91 |
Correct |
47 ms |
18892 KB |
Output is correct |
92 |
Correct |
33 ms |
17932 KB |
Output is correct |
93 |
Correct |
7 ms |
12244 KB |
Output is correct |
94 |
Correct |
5 ms |
12116 KB |
Output is correct |
95 |
Correct |
36 ms |
19060 KB |
Output is correct |
96 |
Correct |
51 ms |
23608 KB |
Output is correct |
97 |
Correct |
6 ms |
12340 KB |
Output is correct |
98 |
Correct |
5 ms |
12116 KB |
Output is correct |
99 |
Correct |
35 ms |
18968 KB |
Output is correct |
100 |
Correct |
6 ms |
12332 KB |
Output is correct |
101 |
Correct |
47 ms |
18380 KB |
Output is correct |
102 |
Correct |
49 ms |
21908 KB |
Output is correct |
103 |
Correct |
6 ms |
12244 KB |
Output is correct |
104 |
Correct |
7 ms |
12312 KB |
Output is correct |
105 |
Correct |
7 ms |
12244 KB |
Output is correct |
106 |
Correct |
6 ms |
12244 KB |
Output is correct |
107 |
Correct |
6 ms |
12244 KB |
Output is correct |
108 |
Correct |
290 ms |
55964 KB |
Output is correct |
109 |
Correct |
556 ms |
63904 KB |
Output is correct |
110 |
Correct |
559 ms |
84720 KB |
Output is correct |
111 |
Correct |
596 ms |
83644 KB |
Output is correct |
112 |
Correct |
537 ms |
71996 KB |
Output is correct |
113 |
Correct |
325 ms |
57148 KB |
Output is correct |
114 |
Correct |
432 ms |
49460 KB |
Output is correct |
115 |
Correct |
437 ms |
49584 KB |
Output is correct |
116 |
Correct |
478 ms |
53688 KB |
Output is correct |
117 |
Correct |
472 ms |
55784 KB |
Output is correct |
118 |
Correct |
429 ms |
51228 KB |
Output is correct |
119 |
Correct |
561 ms |
55872 KB |
Output is correct |
120 |
Correct |
560 ms |
76168 KB |
Output is correct |
121 |
Correct |
529 ms |
55876 KB |
Output is correct |
122 |
Correct |
532 ms |
55084 KB |
Output is correct |
123 |
Correct |
300 ms |
57384 KB |
Output is correct |
124 |
Correct |
608 ms |
54860 KB |
Output is correct |