# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
102476 | AngusRitossa | Mergers (JOI19_mergers) | C++14 | 1553 ms | 154716 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;
#ifdef DEBUG
#define D(x...) printf(x)
#else
#define D(x...)
#endif
int n, k, amof[500010], c[500010];
vector<int> adj[500010];
int leaves;
int last;
map<int, int>* ms[500010];
int dfs(int a, int p = -1)
{
int ambadchildren = 0;
ms[a] = new map<int, int>();
(*ms[a])[c[a]]++;
if ((*ms[a])[c[a]] == amof[c[a]]) ms[a]->erase(c[a]);
for (auto b : adj[a])
{
if (b != p)
{
int am = dfs(b, a);
ambadchildren += am;
if (ms[b]->size() > ms[a]->size()) swap(ms[a], ms[b]);
for (auto c : *ms[b])
{
(*ms[a])[c.first]+=c.second;
if ((*ms[a])[c.first] == amof[c.first]) ms[a]->erase(c.first);
}
}
}
last = max(last, ambadchildren);
if (p != -1)
{
int old = ambadchildren;
ambadchildren = !ms[a]->size();
D("%d %d\n", a, ambadchildren);
if (ambadchildren) last = 1;
if (!old) leaves += ambadchildren;
else ambadchildren = old;
}
else if (p == -1 && ambadchildren == 1)
{
leaves += last == 1;
}
return !!ambadchildren;
}
int main()
{
scanf("%d%d", &n, &k);
for (int i = 1; i < n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) scanf("%d", &c[i]), amof[c[i]]++;
dfs(1);
D("%d\n", leaves);
printf("%d\n", (leaves+1)/2);
}
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... |