#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef pair<int, int> pi;
const char nd = '\n';
void solve();
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t-->0) solve();
}
// ------------------- Code Starts here -------------------
// Union Find
struct UF{
vector<int> p, s;
UF(int n): p(n), s(n, 1){
iota(p.begin(), p.end(), 0);
}
int find(int x){
return p[x] == x ? x : p[x] = find(p[x]);
}
void join(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
if(s[x] < s[y]) swap(x, y);
p[y] = x;
s[x] += s[y];
}
};
const int N = 5e5+7;
vector<int> g[N], state[N];
int s[N], parent[N], level[N];
UF state_group(N);
// dfs to get parent and level of each node
void dfs(int u, int p){
parent[u] = p;
level[u] = level[p]+1;
for (int v : g[u]){
if (v == p) continue;
dfs(v, u);
}
}
void solve(){
int n, k;
cin >> n >> k;
vector<pi> edges;
for (int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
edges.pb({u, v});
g[u].pb(v);
g[v].pb(u);
}
for (int i = 1; i <= n; i++){
cin >> s[i];
state[s[i]].pb(i);
}
dfs(1, 0);
for (int i = 1; i <= n; i++){
int mn = N;
set<pi> st;
for(int u: state[i]) st.insert({-level[u], u});
while(st.size() > 1){
int v;
auto [l, u] = *st.begin();
st.erase(st.begin());
v = parent[u];
state_group.join(s[u], s[v]);
parent[u] = parent[v]; // path compression
st.insert({-level[v], v});
}
}
// degree of each state group
vector<int> deg(n+1, 0);
for(auto [u, v]: edges){
u = state_group.find(s[u]);
v = state_group.find(s[v]);
if (u == v) continue;
deg[u]++;
deg[v]++;
}
int leaf = 0;
for(int i = 1; i <= n; i++){
if(deg[i] == 1) leaf++;
}
cout << (leaf + 1) / 2 << nd;
}
Compilation message
mergers.cpp: In function 'void solve()':
mergers.cpp:85:9: warning: unused variable 'mn' [-Wunused-variable]
85 | int mn = N;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
27740 KB |
Output is correct |
2 |
Incorrect |
9 ms |
27852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
27740 KB |
Output is correct |
2 |
Incorrect |
9 ms |
27852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
27740 KB |
Output is correct |
2 |
Incorrect |
9 ms |
27852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
35520 KB |
Output is correct |
2 |
Correct |
44 ms |
38088 KB |
Output is correct |
3 |
Correct |
14 ms |
27996 KB |
Output is correct |
4 |
Correct |
14 ms |
27980 KB |
Output is correct |
5 |
Correct |
8 ms |
27740 KB |
Output is correct |
6 |
Correct |
9 ms |
29276 KB |
Output is correct |
7 |
Correct |
11 ms |
27996 KB |
Output is correct |
8 |
Correct |
51 ms |
36196 KB |
Output is correct |
9 |
Correct |
10 ms |
27996 KB |
Output is correct |
10 |
Correct |
71 ms |
35272 KB |
Output is correct |
11 |
Incorrect |
8 ms |
27740 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
27740 KB |
Output is correct |
2 |
Incorrect |
9 ms |
27852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |