# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
110825 | IOrtroiii | Mergers (JOI19_mergers) | C++14 | 1137 ms | 69624 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 N = 500500;
int n, m;
int a[N];
pair<int, int> e[N];
vector< pair<int, int> > g[N];
bool br[N];
int num[N], low[N], tt;
void dfs(int u,int p) {
num[u] = low[u] = ++tt;
for (auto nx : g[u]) {
int v, id; tie(v, id) = nx;
if (!num[v]) {
dfs(v, id);
low[u] = min(low[u], low[v]);
if (low[v] > num[u]) br[id] = true;
} else if (id != p) low[u] = min(low[u], num[v]);
}
}
int cnt;
int comp[N];
int deg[N];
void dfs2(int u) {
comp[u] = cnt;
for (auto nx : g[u]) {
int v, id; tie(v, id) = nx;
if (!br[id] && !comp[v]) {
dfs2(v);
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i < n; ++i) {
int u, v; scanf("%d %d", &u, &v);
e[i] = {u, v};
}
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
for (int i = 1; i < n; ++i) {
int u, v; tie(u, v) = e[i];
u = a[u], v = a[v];
if (u == v) continue;
g[u].push_back({v, i});
g[v].push_back({u, i});
}
dfs(1, -1);
for (int i = 1; i <= m; ++i) {
if (!comp[i]) {
++cnt;
dfs2(i);
}
}
for (int i = 1; i <= n; ++i) {
for (auto nx : g[i]) {
int v, id; tie(v, id) = nx;
if (br[id]) ++deg[comp[v]];
}
}
printf("%d\n", (count(deg + 1, deg + cnt + 1, 1) + 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... |