# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
210365 | arman_ferdous | Mergers (JOI19_mergers) | C++14 | 90 ms | 33248 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;
using ll = long long;
const int N = 5e5+10;
int n, k, c[N], dsu[N], deg[N];
vector<int> g[N], node[N];
vector<pair<int,int>> E;
int lev[N], par[N];
int dfs(int u, int f, int d) {
par[u] = f;
lev[u] = d;
for(int v : g[u]) if(v != f)
dfs(v, u, d + 1);
}
int find(int u) {
if(dsu[u] == u) return u;
return dsu[u] = find(dsu[u]);
}
int main() {
scanf("%d %d", &n, &k);
for(int i = 1; i < n; i++) {
int u, v; scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
E.push_back({u, v});
}
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
node[c[i]].push_back(i);
}
iota(dsu, dsu + 1 + n, 0);
dfs(1, 0, 0);
for(int i = 1; i <= k; i++) if((int)node[i].size() > 1) {
for(int j = 1; j < (int)node[i].size(); j++) {
int u = find(node[i][j - 1]), v = find(node[i][j]);
while(u != v) {
if(lev[u] > lev[v]) swap(u, v);
dsu[v] = find(par[v]);
v = dsu[v];
}
}
}
for(auto e : E) {
if(find(e.first) != find(e.second))
deg[e.first]++, deg[e.second]++;
} int cnt = 0;
for(int i = 1; i <= n; ++i) if(deg[i] == 1) cnt++;
printf("%d\n", (cnt + 1) >> 1);
return 0;
}
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... |