#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
int main() {
// ios_base::sync_with_stdio(0);
int n, k;
cin >> n >> k;
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v; cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> s(n);
for (int i = 0; i < n; i++)
cin >> s[i], s[i]--;
if (n == 1) {
cout << 0 << '\n';
return 0;
}
vector<int> tot(n);
for (int x : s)
tot[x]++;
vector<multiset<int>> store(n);
vector<int> id(n);
iota(id.begin(), id.end(), 0);
vector<int> bad(n);
function<void(int, int)> combine = [&](int src, int par) {
if (tot[s[src]] != 1)
store[id[src]].insert(s[src]);
for (int nxt : adj[src]){
if (nxt == par)
continue;
combine(nxt, src);
if ((int) store[id[src]].size() < (int) store[id[nxt]].size())
swap(id[src], id[nxt]);
for (int x : store[id[nxt]]) {
store[id[src]].insert(x);
if (store[id[src]].count(x) == tot[x])
store[id[src]].erase(x);
}
}
if ((int) store[id[src]].size() == 0 && src != 0)
bad[src] = 1;
};
combine(0, -1);
int amt = 0;
for (int i = 1; i < n; i++)
amt += bad[i];
int leaves = 0;
function<int(int, int)> dfs_sub = [&](int src, int par) -> int {
int sub = bad[src];
for (int nxt : adj[src]) {
if (nxt == par)
continue;
sub += dfs_sub(nxt, src);
}
if (sub == 1 && bad[src])
leaves++;
else if (sub == amt && bad[src])
leaves++;
return sub;
};
dfs_sub(0, -1);
int ans = (leaves + 1) / 2;
cout << ans << '\n';
return 0;
}
Compilation message
mergers.cpp: In lambda function:
mergers.cpp:46:45: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
46 | if (store[id[src]].count(x) == tot[x])
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
4 ms |
748 KB |
Output is correct |
27 |
Correct |
5 ms |
876 KB |
Output is correct |
28 |
Correct |
5 ms |
900 KB |
Output is correct |
29 |
Correct |
4 ms |
748 KB |
Output is correct |
30 |
Correct |
7 ms |
1260 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
4 ms |
1004 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
8 ms |
1004 KB |
Output is correct |
35 |
Correct |
4 ms |
748 KB |
Output is correct |
36 |
Correct |
5 ms |
1004 KB |
Output is correct |
37 |
Correct |
5 ms |
1004 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
6 ms |
1132 KB |
Output is correct |
40 |
Correct |
41 ms |
1388 KB |
Output is correct |
41 |
Correct |
8 ms |
1132 KB |
Output is correct |
42 |
Correct |
6 ms |
876 KB |
Output is correct |
43 |
Correct |
31 ms |
1388 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
4 ms |
748 KB |
Output is correct |
46 |
Correct |
5 ms |
876 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
4 ms |
876 KB |
Output is correct |
49 |
Correct |
3 ms |
620 KB |
Output is correct |
50 |
Correct |
4 ms |
748 KB |
Output is correct |
51 |
Correct |
4 ms |
876 KB |
Output is correct |
52 |
Correct |
6 ms |
1004 KB |
Output is correct |
53 |
Correct |
5 ms |
748 KB |
Output is correct |
54 |
Correct |
7 ms |
1260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Execution timed out |
3071 ms |
20484 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3069 ms |
20528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
4 ms |
748 KB |
Output is correct |
27 |
Correct |
5 ms |
876 KB |
Output is correct |
28 |
Correct |
5 ms |
900 KB |
Output is correct |
29 |
Correct |
4 ms |
748 KB |
Output is correct |
30 |
Correct |
7 ms |
1260 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
4 ms |
1004 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
8 ms |
1004 KB |
Output is correct |
35 |
Correct |
4 ms |
748 KB |
Output is correct |
36 |
Correct |
5 ms |
1004 KB |
Output is correct |
37 |
Correct |
5 ms |
1004 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
6 ms |
1132 KB |
Output is correct |
40 |
Correct |
41 ms |
1388 KB |
Output is correct |
41 |
Correct |
8 ms |
1132 KB |
Output is correct |
42 |
Correct |
6 ms |
876 KB |
Output is correct |
43 |
Correct |
31 ms |
1388 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
4 ms |
748 KB |
Output is correct |
46 |
Correct |
5 ms |
876 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
4 ms |
876 KB |
Output is correct |
49 |
Correct |
3 ms |
620 KB |
Output is correct |
50 |
Correct |
4 ms |
748 KB |
Output is correct |
51 |
Correct |
4 ms |
876 KB |
Output is correct |
52 |
Correct |
6 ms |
1004 KB |
Output is correct |
53 |
Correct |
5 ms |
748 KB |
Output is correct |
54 |
Correct |
7 ms |
1260 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Execution timed out |
3071 ms |
20484 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |