This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// subtask 1 && subtask 2
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define INF 1e9
using namespace std;
typedef long long ll;
int v[200005], p[200005];
vector<int>adj[200005], c[200005];
void dfs(int s, int e) {
for (auto u : adj[s]) {
if (u == e) continue;
p[u] = s;
dfs(u, s);
}
}
void solve()
{
int n, k; cin >> n >> k;
for (int i = 1; i <= n - 1; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
cin >> v[i];
c[v[i]].push_back(i);
}
dfs(1, 0);
int ans = INF;
for (int i = 1; i <= k; i++) {
queue<int>q;
for (auto u : c[i]) {q.push(u); }
vector<bool>visited(k + 1, false);
visited[i] = true;
int anss = 0;
while (q.size()) {
int u = q.front(); q.pop();
if (p[u] != 0 && v[p[u]] != v[u] && !visited[v[p[u]]]) {
visited[v[p[u]]] = true;
++anss;
for (auto x : c[v[p[u]]])q.push(x);
}
}
ans = min(ans, anss);
}
cout << ans << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;//cin>>t;
while (t--) solve();
return 0;
}
# | 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... |