# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
210176 | arman_ferdous | Mergers (JOI19_mergers) | C++17 | 75 ms | 30956 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];
vector<int> g[N], node[N];
int cur, vis[N], comp[N];
set<int> st;
int p[N];
void dfs(int u, int f) {
p[u] = f;
for(int v : g[u]) if(v != f)
dfs(v, u);
}
void up(int u) {
if(vis[u] != 0) return;
vis[u] = cur;
st.insert(c[u]);
if(p[u] + 1) up(p[u]);
}
void process() {
while(!st.empty()) {
int x = *st.begin();
st.erase(st.begin());
for(int u : node[x])
up(u);
node[x].clear();
}
}
int deg[N];
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);
}
dfs(1, -1);
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
node[c[i]].push_back(i);
}
for(int i = 1; i <= k; i++) if(!node[i].empty()) {
cur++;
st.insert(i);
process();
}
for(int i = 2; i <= n; i++) if(vis[p[i]] != vis[i]) {
deg[vis[p[i]]]++;
deg[i]++;
}
int leaf = 0;
for(int i = 1; i <= n; i++) if(deg[i] == 1) leaf++;
if(leaf == 0) puts("0");
else printf("%d\n", (leaf - 1) / 2 + 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... |