//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], root, cnt[N];
vector < int > g[N];
vector < int > mark[N];
void dfs (int v, int p) {
sub[v] = 1; mark[v][a[v]] = 1;
for (auto u : g[v]) {
if (u == p) continue;
dfs(u, v);
sub[v] += sub[u];
cnt[v] += cnt[u];
for (int i = 1; i <= k; i++) mark[v][i] |= mark[u][i];
}
ll x = 0;
for (int y = 1; y <= k; y++) {
if (mark[v][y]) x += c[y];
}
if ((x == 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++) {
mark[i].resize(k + 1, 0);
}
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;
}
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 |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
2 ms |
5028 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
4968 KB |
Output is correct |
14 |
Correct |
3 ms |
5036 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
2 ms |
5032 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
2 ms |
5028 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
4968 KB |
Output is correct |
14 |
Correct |
3 ms |
5036 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
2 ms |
5032 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
5032 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
26 ms |
28764 KB |
Output is correct |
27 |
Correct |
3 ms |
5844 KB |
Output is correct |
28 |
Correct |
31 ms |
40436 KB |
Output is correct |
29 |
Correct |
32 ms |
40488 KB |
Output is correct |
30 |
Correct |
5 ms |
7508 KB |
Output is correct |
31 |
Correct |
2 ms |
4948 KB |
Output is correct |
32 |
Correct |
31 ms |
40660 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |
34 |
Correct |
3 ms |
5808 KB |
Output is correct |
35 |
Correct |
31 ms |
40452 KB |
Output is correct |
36 |
Correct |
4 ms |
5844 KB |
Output is correct |
37 |
Correct |
18 ms |
22868 KB |
Output is correct |
38 |
Correct |
2 ms |
5032 KB |
Output is correct |
39 |
Correct |
22 ms |
22868 KB |
Output is correct |
40 |
Correct |
3 ms |
5204 KB |
Output is correct |
41 |
Correct |
3 ms |
5300 KB |
Output is correct |
42 |
Correct |
20 ms |
22868 KB |
Output is correct |
43 |
Correct |
3 ms |
5460 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
30 ms |
39252 KB |
Output is correct |
46 |
Correct |
22 ms |
22484 KB |
Output is correct |
47 |
Correct |
2 ms |
4948 KB |
Output is correct |
48 |
Correct |
10 ms |
12244 KB |
Output is correct |
49 |
Correct |
32 ms |
40492 KB |
Output is correct |
50 |
Correct |
32 ms |
40576 KB |
Output is correct |
51 |
Correct |
13 ms |
12244 KB |
Output is correct |
52 |
Correct |
3 ms |
5844 KB |
Output is correct |
53 |
Correct |
20 ms |
22856 KB |
Output is correct |
54 |
Correct |
4 ms |
5844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
2 ms |
5028 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
4968 KB |
Output is correct |
14 |
Correct |
3 ms |
5036 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
2 ms |
5032 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
5032 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
66 ms |
32680 KB |
Output is correct |
27 |
Correct |
82 ms |
32728 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
2 ms |
4948 KB |
Output is correct |
30 |
Correct |
2 ms |
4948 KB |
Output is correct |
31 |
Correct |
79 ms |
32932 KB |
Output is correct |
32 |
Correct |
4 ms |
5844 KB |
Output is correct |
33 |
Correct |
114 ms |
41400 KB |
Output is correct |
34 |
Correct |
83 ms |
32736 KB |
Output is correct |
35 |
Correct |
4 ms |
5844 KB |
Output is correct |
36 |
Correct |
93 ms |
33740 KB |
Output is correct |
37 |
Correct |
3 ms |
5204 KB |
Output is correct |
38 |
Correct |
3 ms |
5300 KB |
Output is correct |
39 |
Correct |
67 ms |
32556 KB |
Output is correct |
40 |
Correct |
3 ms |
5588 KB |
Output is correct |
41 |
Correct |
43 ms |
13912 KB |
Output is correct |
42 |
Correct |
90 ms |
35964 KB |
Output is correct |
43 |
Correct |
2 ms |
4948 KB |
Output is correct |
44 |
Correct |
54 ms |
22896 KB |
Output is correct |
45 |
Correct |
88 ms |
37252 KB |
Output is correct |
46 |
Correct |
4 ms |
5844 KB |
Output is correct |
47 |
Correct |
5 ms |
5812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
32624 KB |
Output is correct |
2 |
Runtime error |
140 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
2 ms |
5028 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
4968 KB |
Output is correct |
14 |
Correct |
3 ms |
5036 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
2 ms |
5032 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
5032 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
26 ms |
28764 KB |
Output is correct |
27 |
Correct |
3 ms |
5844 KB |
Output is correct |
28 |
Correct |
31 ms |
40436 KB |
Output is correct |
29 |
Correct |
32 ms |
40488 KB |
Output is correct |
30 |
Correct |
5 ms |
7508 KB |
Output is correct |
31 |
Correct |
2 ms |
4948 KB |
Output is correct |
32 |
Correct |
31 ms |
40660 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |
34 |
Correct |
3 ms |
5808 KB |
Output is correct |
35 |
Correct |
31 ms |
40452 KB |
Output is correct |
36 |
Correct |
4 ms |
5844 KB |
Output is correct |
37 |
Correct |
18 ms |
22868 KB |
Output is correct |
38 |
Correct |
2 ms |
5032 KB |
Output is correct |
39 |
Correct |
22 ms |
22868 KB |
Output is correct |
40 |
Correct |
3 ms |
5204 KB |
Output is correct |
41 |
Correct |
3 ms |
5300 KB |
Output is correct |
42 |
Correct |
20 ms |
22868 KB |
Output is correct |
43 |
Correct |
3 ms |
5460 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
30 ms |
39252 KB |
Output is correct |
46 |
Correct |
22 ms |
22484 KB |
Output is correct |
47 |
Correct |
2 ms |
4948 KB |
Output is correct |
48 |
Correct |
10 ms |
12244 KB |
Output is correct |
49 |
Correct |
32 ms |
40492 KB |
Output is correct |
50 |
Correct |
32 ms |
40576 KB |
Output is correct |
51 |
Correct |
13 ms |
12244 KB |
Output is correct |
52 |
Correct |
3 ms |
5844 KB |
Output is correct |
53 |
Correct |
20 ms |
22856 KB |
Output is correct |
54 |
Correct |
4 ms |
5844 KB |
Output is correct |
55 |
Correct |
3 ms |
4948 KB |
Output is correct |
56 |
Correct |
66 ms |
32680 KB |
Output is correct |
57 |
Correct |
82 ms |
32728 KB |
Output is correct |
58 |
Correct |
4 ms |
5844 KB |
Output is correct |
59 |
Correct |
2 ms |
4948 KB |
Output is correct |
60 |
Correct |
2 ms |
4948 KB |
Output is correct |
61 |
Correct |
79 ms |
32932 KB |
Output is correct |
62 |
Correct |
4 ms |
5844 KB |
Output is correct |
63 |
Correct |
114 ms |
41400 KB |
Output is correct |
64 |
Correct |
83 ms |
32736 KB |
Output is correct |
65 |
Correct |
4 ms |
5844 KB |
Output is correct |
66 |
Correct |
93 ms |
33740 KB |
Output is correct |
67 |
Correct |
3 ms |
5204 KB |
Output is correct |
68 |
Correct |
3 ms |
5300 KB |
Output is correct |
69 |
Correct |
67 ms |
32556 KB |
Output is correct |
70 |
Correct |
3 ms |
5588 KB |
Output is correct |
71 |
Correct |
43 ms |
13912 KB |
Output is correct |
72 |
Correct |
90 ms |
35964 KB |
Output is correct |
73 |
Correct |
2 ms |
4948 KB |
Output is correct |
74 |
Correct |
54 ms |
22896 KB |
Output is correct |
75 |
Correct |
88 ms |
37252 KB |
Output is correct |
76 |
Correct |
4 ms |
5844 KB |
Output is correct |
77 |
Correct |
5 ms |
5812 KB |
Output is correct |
78 |
Correct |
68 ms |
32624 KB |
Output is correct |
79 |
Runtime error |
140 ms |
262144 KB |
Execution killed with signal 9 |
80 |
Halted |
0 ms |
0 KB |
- |