# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
757194 | 2023-06-12T19:00:55 Z | sheldon | Mergers (JOI19_mergers) | C++14 | 122 ms | 50576 KB |
#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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 20 ms | 39380 KB | Output is correct |
2 | Incorrect | 20 ms | 39508 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 20 ms | 39380 KB | Output is correct |
2 | Incorrect | 20 ms | 39508 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 20 ms | 39380 KB | Output is correct |
2 | Incorrect | 20 ms | 39508 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 71 ms | 44820 KB | Output is correct |
2 | Incorrect | 122 ms | 50576 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 20 ms | 39380 KB | Output is correct |
2 | Incorrect | 20 ms | 39508 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |