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 MAXN = 5e5 + 10;
vector<int> adj[MAXN];
int fin[MAXN];
int s[MAXN];
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
vector<int> e;
int dep[MAXN];
int tin[MAXN];
int tim = 0;
void dfs1(int node, int prev, int cur) {
e.push_back(node);
dep[node] = cur;
tin[node] = ++tim;
for(int x: adj[node]) {
if(x != prev) {
dfs1(x, node, cur + 1);
e.push_back(node);
}
}
}
pair<int, int> st[20][2*MAXN];
int l[MAXN], r[MAXN];
int lca(int x, int y) {
int m1 = min(l[x], l[y]);
int m2 = max(r[x], r[y]);
int k = 32 - __builtin_clz(m2 - m1 + 1) - 1;
return min(st[k][m1], st[k][m2 - (1<<k) + 1]).second;
}
int delta[MAXN];
bool cmp(int a, int b) {
return tin[a] < tin[b];
}
map<pair<int, int>, bool> lit;
int dfs2(int node, int prev) {
int s = delta[node];
for(int x: adj[node]) {
if(x != prev) s += dfs2(x, node);
}
if(s == 0) lit[{node, prev}] = lit[{prev, node}] = 1;
return s;
}
int32_t main() {
int n,k;cin >> n >> k;
for(int i=1;i<n;i++) {
int a,b;cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1; i<=n; i++) cin >> s[i];
for(int i=0; i<=n; i++) dsu[i] = i;
dfs1(1, -1, 0);
for(int i=0; i<e.size(); i++) r[e[i]] = i;
for(int i=e.size()-1; i>=0; i--) l[e[i]] = i;
for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[i]};
for(int i=1; i<20; i++) {
for(int j=0; j<e.size(); j++) {
if(j + (1<<i) - 1 < e.size()) {
st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
}
}
}
vector<int> wow[k+1];
for(int i=1; i<=n; i++) {
wow[s[i]].push_back(i);
}
for(int i=1; i<=k; i++) {
if(wow[i].size() == 1) continue;
sort(wow[i].begin(), wow[i].end(), cmp);
int l = lca(wow[i][0], wow[i][1]);
for(int j=0; j<wow[i].size(); j++) {
l = lca(l, wow[i][j]);
delta[wow[i][j]]++;
}
delta[l] -= wow[i].size();
}
dfs2(1, -1);
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(!lit[{i, x}]) {
union_(i, x);
}
}
}
lit.clear();
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(set_of(i) != set_of(x) && lit[{set_of(i), set_of(x)}] == 0) {
lit[{set_of(i), set_of(x)}] = 1;
lit[{set_of(x), set_of(i)}] = 1;
fin[set_of(i)]++;
fin[set_of(x)]++;
}
}
}
int s = 0;
for(int i=1; i<=n; i++) {
s += (fin[i] == 1);
}
cout << (s + 1) / 2 << "\n";
}
Compilation message (stderr)
mergers.cpp: In function 'int32_t main()':
mergers.cpp:61:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i=0; i<e.size(); i++) r[e[i]] = i;
| ~^~~~~~~~~
mergers.cpp:63:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[i]};
| ~^~~~~~~~~
mergers.cpp:65:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int j=0; j<e.size(); j++) {
| ~^~~~~~~~~
mergers.cpp:66:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if(j + (1<<i) - 1 < e.size()) {
| ~~~~~~~~~~~~~~~^~~~~~~~~~
mergers.cpp:79:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int j=0; j<wow[i].size(); j++) {
| ~^~~~~~~~~~~~~~
# | 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... |