// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 2e5 + 7;
int n, k, a[N], sz[N], par[N], del[N], dsu[N], rep[N], h[N];
int ans = inf;
bool mark[N], vis[N];
vector<int> col[N], g[N];
vector<int> nodes;
int find(int u) {
if(dsu[u] < 0) return u;
return dsu[u] = find(dsu[u]);
}
void join(int u, int v) {
u = find(u), v = find(v);
if(u == v) return;
if(dsu[u] > dsu[v]) swap(u, v);
rep[u] = (h[rep[u]] < h[rep[v]]) ? rep[u] : rep[v];
dsu[u] += dsu[v];
dsu[v] = u;
}
void count(int u, int p) {
sz[u] = 1;
for(int v : g[u]) {
if(v == p or del[v]) continue;
count(v, u);
sz[u] += sz[v];
}
}
int find_cen(int u, int p, int n) {
for(int v : g[u]) {
if(del[v] or v == p) continue;
if(sz[v] * 2 > n) {
return find_cen(v, u, n);
}
}
return u;
}
void dfs(int u, int p) {
mark[u] = 1;
par[u] = p;
h[u] = h[p] + 1;
nodes.pb(u);
for(int v : g[u]) {
if(v == p or del[v]) continue;
dfs(v, u);
}
}
void solve(int u) {
count(u, 0);
int root = find_cen(u, 0, sz[u]);
dfs(root, 0);
for(int x : nodes) {
dsu[x] = -1;
rep[x] = x;
}
bool ok = 1;
queue<int> q;
q.push(a[root]);
vis[a[root]] = 1;
int cur = 0;
while(sz(q)) {
int c = q.front(); q.pop();
int pre = col[c][0];
cur++;
if(!mark[pre]) {
ok = 0;
break;
}
for(int i = 1; i < sz(col[c]); i++) {
int u = col[c][i];
if(!mark[u]) {
ok = 0;
break;
}
// cerr << u << ' ' << pre << ' ' << rep[u] << ' ' << rep[pre] << ln;
while(u != pre) {
// cerr << u << ' ' << pre << ' ' << h[u] << ' ' << h[pre] << ln;
if(h[u] > h[pre]) {
join(u, par[u]);
u = rep[find(u)];
if(!vis[a[u]]) {
q.push(a[u]);
vis[a[u]] = 1;
}
} else {
join(pre, par[pre]);
pre = rep[find(pre)];
if(!vis[a[pre]]) {
q.push(a[pre]);
vis[a[pre]] = 1;
}
}
}
}
if(!ok) break;
}
if(ok) {
ans = min(ans, cur - 1);
// cerr << "OK " << ln;
// for(int x : nodes) {
// cerr << x << ' ';
// }
// cerr << cur << ln;
}
for(int x : nodes) {
mark[x] = 0;
vis[a[x]] = 0;
}
nodes.clear();
del[root] = 1;
for(int v : g[root]) {
if(del[v]) continue;
solve(v);
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n >> k;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; i++) {
cin >> a[i];
col[a[i]].pb(i);
}
solve(1);
cout << ans << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
capital_city.cpp: In function 'int main()':
capital_city.cpp:140:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:141:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | 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... |