#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
const int N = 5e5 + 69;
const int K = 20;
int n, k, a[N], ans, cnt[N], dep[N], up[N], is[N];
vector<int> g[N], c[N];
vector<int> t[N];
int have[N][52];
void dfs(int v, int p) {
have[v][a[v]] = 1;
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
for (int j = 1;j <= k;j++)
have[v][j] += have[u][j];
}
int ok = 1;
for (int j = 1;j <= k;j++) {
if (have[v][j] > 0)
ok &= (have[v][j] == (int)c[j].size());
}
cnt[v] = ok;
}
void build(int v, int p, int c) {
int x = c;
if (cnt[v] && v > 1) {
t[v].push_back(c);
t[c].push_back(v);
up[v] = c;
x = v;
}
for (auto u : g[v]) {
if (u == p)
continue;
build(u, v, x);
}
}
void calc(int v, int p) {
if (t[v].size() == 1 && v > 1)
is[v] = 1;
for (auto u : t[v]) {
if (u == p)
continue;
calc(u, v);
is[v] += is[u];
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> k;
for (int i = 1;i <= n - 1;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1;i <= n;i++) {
cin >> a[i];
c[a[i]].push_back(i);
}
dfs(1, 0);
build(1, 0, 1);
calc(1, 0);
assert(cnt[1] == 1);
int o = 0;
for (int i = 1;i <= n;i++) {
if (cnt[i] && t[i].size() == 1)
o += 1;
}
ans = (o + 1) / 2;
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
43352 KB |
Output is correct |
2 |
Correct |
10 ms |
43352 KB |
Output is correct |
3 |
Correct |
11 ms |
43356 KB |
Output is correct |
4 |
Correct |
14 ms |
43356 KB |
Output is correct |
5 |
Correct |
12 ms |
41432 KB |
Output is correct |
6 |
Correct |
13 ms |
41304 KB |
Output is correct |
7 |
Correct |
12 ms |
41308 KB |
Output is correct |
8 |
Correct |
12 ms |
41308 KB |
Output is correct |
9 |
Correct |
13 ms |
43356 KB |
Output is correct |
10 |
Correct |
13 ms |
43708 KB |
Output is correct |
11 |
Correct |
11 ms |
43352 KB |
Output is correct |
12 |
Correct |
11 ms |
43356 KB |
Output is correct |
13 |
Correct |
11 ms |
43352 KB |
Output is correct |
14 |
Correct |
10 ms |
43356 KB |
Output is correct |
15 |
Correct |
15 ms |
43612 KB |
Output is correct |
16 |
Correct |
13 ms |
43356 KB |
Output is correct |
17 |
Correct |
12 ms |
43356 KB |
Output is correct |
18 |
Correct |
10 ms |
41444 KB |
Output is correct |
19 |
Correct |
13 ms |
43356 KB |
Output is correct |
20 |
Correct |
10 ms |
43356 KB |
Output is correct |
21 |
Correct |
13 ms |
41308 KB |
Output is correct |
22 |
Correct |
12 ms |
41552 KB |
Output is correct |
23 |
Correct |
9 ms |
41308 KB |
Output is correct |
24 |
Correct |
10 ms |
43356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
43352 KB |
Output is correct |
2 |
Correct |
10 ms |
43352 KB |
Output is correct |
3 |
Correct |
11 ms |
43356 KB |
Output is correct |
4 |
Correct |
14 ms |
43356 KB |
Output is correct |
5 |
Correct |
12 ms |
41432 KB |
Output is correct |
6 |
Correct |
13 ms |
41304 KB |
Output is correct |
7 |
Correct |
12 ms |
41308 KB |
Output is correct |
8 |
Correct |
12 ms |
41308 KB |
Output is correct |
9 |
Correct |
13 ms |
43356 KB |
Output is correct |
10 |
Correct |
13 ms |
43708 KB |
Output is correct |
11 |
Correct |
11 ms |
43352 KB |
Output is correct |
12 |
Correct |
11 ms |
43356 KB |
Output is correct |
13 |
Correct |
11 ms |
43352 KB |
Output is correct |
14 |
Correct |
10 ms |
43356 KB |
Output is correct |
15 |
Correct |
15 ms |
43612 KB |
Output is correct |
16 |
Correct |
13 ms |
43356 KB |
Output is correct |
17 |
Correct |
12 ms |
43356 KB |
Output is correct |
18 |
Correct |
10 ms |
41444 KB |
Output is correct |
19 |
Correct |
13 ms |
43356 KB |
Output is correct |
20 |
Correct |
10 ms |
43356 KB |
Output is correct |
21 |
Correct |
13 ms |
41308 KB |
Output is correct |
22 |
Correct |
12 ms |
41552 KB |
Output is correct |
23 |
Correct |
9 ms |
41308 KB |
Output is correct |
24 |
Correct |
10 ms |
43356 KB |
Output is correct |
25 |
Correct |
14 ms |
43356 KB |
Output is correct |
26 |
Runtime error |
81 ms |
92836 KB |
Execution killed with signal 6 |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
43352 KB |
Output is correct |
2 |
Correct |
10 ms |
43352 KB |
Output is correct |
3 |
Correct |
11 ms |
43356 KB |
Output is correct |
4 |
Correct |
14 ms |
43356 KB |
Output is correct |
5 |
Correct |
12 ms |
41432 KB |
Output is correct |
6 |
Correct |
13 ms |
41304 KB |
Output is correct |
7 |
Correct |
12 ms |
41308 KB |
Output is correct |
8 |
Correct |
12 ms |
41308 KB |
Output is correct |
9 |
Correct |
13 ms |
43356 KB |
Output is correct |
10 |
Correct |
13 ms |
43708 KB |
Output is correct |
11 |
Correct |
11 ms |
43352 KB |
Output is correct |
12 |
Correct |
11 ms |
43356 KB |
Output is correct |
13 |
Correct |
11 ms |
43352 KB |
Output is correct |
14 |
Correct |
10 ms |
43356 KB |
Output is correct |
15 |
Correct |
15 ms |
43612 KB |
Output is correct |
16 |
Correct |
13 ms |
43356 KB |
Output is correct |
17 |
Correct |
12 ms |
43356 KB |
Output is correct |
18 |
Correct |
10 ms |
41444 KB |
Output is correct |
19 |
Correct |
13 ms |
43356 KB |
Output is correct |
20 |
Correct |
10 ms |
43356 KB |
Output is correct |
21 |
Correct |
13 ms |
41308 KB |
Output is correct |
22 |
Correct |
12 ms |
41552 KB |
Output is correct |
23 |
Correct |
9 ms |
41308 KB |
Output is correct |
24 |
Correct |
10 ms |
43356 KB |
Output is correct |
25 |
Correct |
13 ms |
43352 KB |
Output is correct |
26 |
Correct |
88 ms |
90568 KB |
Output is correct |
27 |
Correct |
123 ms |
90964 KB |
Output is correct |
28 |
Correct |
15 ms |
45912 KB |
Output is correct |
29 |
Correct |
11 ms |
43356 KB |
Output is correct |
30 |
Correct |
14 ms |
43356 KB |
Output is correct |
31 |
Correct |
113 ms |
90388 KB |
Output is correct |
32 |
Correct |
14 ms |
43864 KB |
Output is correct |
33 |
Correct |
138 ms |
97620 KB |
Output is correct |
34 |
Correct |
117 ms |
90344 KB |
Output is correct |
35 |
Correct |
15 ms |
45912 KB |
Output is correct |
36 |
Correct |
128 ms |
90028 KB |
Output is correct |
37 |
Correct |
14 ms |
43868 KB |
Output is correct |
38 |
Correct |
15 ms |
46172 KB |
Output is correct |
39 |
Correct |
95 ms |
90436 KB |
Output is correct |
40 |
Correct |
14 ms |
44120 KB |
Output is correct |
41 |
Correct |
79 ms |
90092 KB |
Output is correct |
42 |
Correct |
141 ms |
91220 KB |
Output is correct |
43 |
Correct |
13 ms |
43352 KB |
Output is correct |
44 |
Correct |
118 ms |
97732 KB |
Output is correct |
45 |
Correct |
146 ms |
96080 KB |
Output is correct |
46 |
Correct |
12 ms |
43868 KB |
Output is correct |
47 |
Correct |
15 ms |
43868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
90032 KB |
Output is correct |
2 |
Execution timed out |
3029 ms |
89008 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
43352 KB |
Output is correct |
2 |
Correct |
10 ms |
43352 KB |
Output is correct |
3 |
Correct |
11 ms |
43356 KB |
Output is correct |
4 |
Correct |
14 ms |
43356 KB |
Output is correct |
5 |
Correct |
12 ms |
41432 KB |
Output is correct |
6 |
Correct |
13 ms |
41304 KB |
Output is correct |
7 |
Correct |
12 ms |
41308 KB |
Output is correct |
8 |
Correct |
12 ms |
41308 KB |
Output is correct |
9 |
Correct |
13 ms |
43356 KB |
Output is correct |
10 |
Correct |
13 ms |
43708 KB |
Output is correct |
11 |
Correct |
11 ms |
43352 KB |
Output is correct |
12 |
Correct |
11 ms |
43356 KB |
Output is correct |
13 |
Correct |
11 ms |
43352 KB |
Output is correct |
14 |
Correct |
10 ms |
43356 KB |
Output is correct |
15 |
Correct |
15 ms |
43612 KB |
Output is correct |
16 |
Correct |
13 ms |
43356 KB |
Output is correct |
17 |
Correct |
12 ms |
43356 KB |
Output is correct |
18 |
Correct |
10 ms |
41444 KB |
Output is correct |
19 |
Correct |
13 ms |
43356 KB |
Output is correct |
20 |
Correct |
10 ms |
43356 KB |
Output is correct |
21 |
Correct |
13 ms |
41308 KB |
Output is correct |
22 |
Correct |
12 ms |
41552 KB |
Output is correct |
23 |
Correct |
9 ms |
41308 KB |
Output is correct |
24 |
Correct |
10 ms |
43356 KB |
Output is correct |
25 |
Correct |
14 ms |
43356 KB |
Output is correct |
26 |
Runtime error |
81 ms |
92836 KB |
Execution killed with signal 6 |
27 |
Halted |
0 ms |
0 KB |
- |