# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
757194 | sheldon | Mergers (JOI19_mergers) | C++14 | 122 ms | 50576 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int nax = 5e5 + 5;
vector<int> edges[nax];
int state[nax], cnt[nax], sz[nax];
struct block {
int have = 0;
map<int, int> mp;
};
block bla[nax];
void merge (block &a, block &b) {
if (a.mp.size() > b.mp.size()) {
swap(a, b);
}
for (auto &it : a.mp) {
b.mp[it.first] += it.second;
if (b.mp[it.first] == cnt[it.first]) {
++b.have;
}
}
a.mp.clear();
}
int ans = 0;
void dfs (int u, int p) {
bla[u].mp[state[u]]++;
if (cnt[state[u]] == 1) ++bla[u].have;
for (int v : edges[u]) {
if (v != p) {
dfs (v, u);
sz[u] += sz[v];
merge(bla[v], bla[u]);
}
}
if (bla[u].mp.size() == bla[u].have) {
sz[u]++;
}
if (sz[u] == 1) ++ans;
}
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
cin >> state[i];
cnt[state[i]]++;
}
dfs (1, 0);
if (sz[1] == 1) {
cout << "0\n";
} else {
cout << (ans + 1) / 2 << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |