#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)5e5 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
int par[N], h[N];
int p[N], z[N], high[N];
vector<int> adj[N];
vector<int> r[N];
void dfs(int u, int p) {
for(int v : adj[u]) if (v != p) {
h[v] = h[u] + 1;
par[v] = u;
dfs(v, u);
}
}
int root(int u) {
return u == p[u] ? u : p[u] = root(p[u]);
}
void unite(int u, int v) {
u=root(u), v=root(v);
// cerr << "u,v: " << u << ' ' << v << nl;
if (z[u] < z[v]) swap(u, v);
z[u] += z[v], high[u] = (h[high[u]] < h[high[v]] ? high[u] : high[v]);
p[v] = u;
}
void unite2(int u, int v) {
// cerr << u << ' ' << v << nl;
u = root(u), v = root(v);
if (u == v) return ;
u = high[u], v = high[v];
if (h[u] == h[v]) {
unite2(par[u], par[v]);
unite(u, par[u]);
unite(v, par[v]);
return ;
}
if (h[u] < h[v]) swap(u, v);
unite2(par[u], v);
unite(u, par[u]);
}
vector<int> adj2[N];
void main_code() {
int n, k; cin >> n >> k;
FOR(i, 1, n - 1) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; ++i) {
int x; cin >> x; r[x].push_back(i);
}
FOR(i, 1, n) p[i] = i, high[i] = i, z[i] = 1;
dfs(1, 0);
FOR(i, 1, k) {
for(int j = 0; j + 1 < sz(r[i]); ++j) {
unite2(r[i][j], r[i][j + 1]);
}
}
int ans = 0;
FOR(i, 1, n) {
for(int j : adj[i]) if (i < j) {
if (root(i) != root(j)) {
adj2[root(i)].push_back(root(j));
adj2[root(j)].push_back(root(i));
}
}
}
FOR(i, 1, n) {
unq(adj2[i]);
ans += adj2[i].size() == 1;
}
cout << (ans + 1) / 2;
}
/* Let the river flows naturally */
컴파일 시 표준 에러 (stderr) 메시지
mergers.cpp: In function 'int main()':
mergers.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |