# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
545531 | LucaDantas | Mergers (JOI19_mergers) | C++17 | 163 ms | 66448 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;
constexpr int maxn = 5e5+10, logn = 21;
struct DSU {
int pai[maxn], peso[maxn], sz;
DSU(int n = 0, int _sz = 0) {
sz = _sz;
for(int i = 1; i <= n; i++)
pai[i] = i, peso[i] = 1;
}
int find(int x) { return pai[x] == x ? x : pai[x] = find(pai[x]); }
void join(int a, int b) {
a = find(a), b = find(b);
if(a == b) return;
--sz;
if(peso[a] < peso[b])
swap(a, b);
pai[b] = a;
peso[a] += peso[b];
}
} dsu;
vector<int> g[maxn];
int p[maxn][logn], depth[maxn];
void pre(int u) {
for(int v : g[u]) if(v != p[u][0]) {
p[v][0] = u;
depth[v] = depth[u]+1;
pre(v);
}
}
void build() {
for(int l = 1; l < logn; l++)
for(int u = 0; u < maxn; u++)
p[u][l] = p[p[u][l-1]][l-1];
}
int LCA(int a, int b) {
if(depth[a] < depth[b])
swap(a, b); // a é o que sobe
for(int l = logn-1; l >= 0; l--)
if(depth[a] - (1 << l) >= depth[b])
a = p[a][l];
if(a == b) return a;
for(int l = logn-1; l >= 0; l--)
if(p[a][l] != p[b][l])
a = p[a][l], b = p[b][l];
return p[a][0];
}
int lca_cor[maxn], lca[maxn], cor[maxn];
void dfs(int u) {
lca[u] = lca_cor[cor[u]];
for(int v : g[u]) if(v != p[u][0]) {
dfs(v);
if(lca[v] == v) continue;
dsu.join(cor[u], cor[v]);
lca[u] = LCA(lca[u], lca[v]);
}
}
int main() {
int n, k; scanf("%d %d", &n, &k);
for(int i = 1, u, v; i < n; i++)
scanf("%d %d", &u, &v), g[u].push_back(v), g[v].push_back(u);
pre(1);
build();
for(int i = 1; i <= n; i++)
scanf("%d", &cor[i]);
for(int i = 1; i <= n; i++) {
if(!lca_cor[cor[i]])
lca_cor[cor[i]] = i;
else
lca_cor[cor[i]] = LCA(lca_cor[cor[i]], i);
}
dsu = DSU(n, k);
dfs(1);
printf("%d\n", dsu.sz >> 1);
}
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... |