//#include"holiday.h"
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define REB(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;///lon
const ll INF = 1e18 + 7;
const ll BASE = 137;
const int szBL = 450;
int n, K;
int a[N];
vector<int> ke[N], adj[N], vec[N];
int high[N], par[N][25], Anc[N];
int num[N], low[N], del[N], SCC[N], nSCC, sz[N];
int dp[N];
void pdfs (int u, int p) {
high[u] = high[p] + 1;
par[u][0] = p;
rep (i, 1, 20) par[u][i] = par[par[u][i - 1]][i - 1];
iter (&v, ke[u]) {
if (v != p) {
pdfs(v, u);
}
}
}
int LCA (int u, int v) {
if (high[u] > high[v]) swap(u, v);
rep (i, 0, 20) if (bit(high[v] - high[u], i)) v = par[v][i];
if (v == u) return v;
REB (i, 20, 0) if (par[v][i] != par[u][i]) {
v = par[v][i];
u = par[u][i];
}
return par[v][0];
}
void solution() {
cin >> n >> K;
rep (i, 2, n) {
int u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
pdfs(1, 0);
rep (i, 1, n) {
int X;
cin >> X;
a[i] = X;
vec[X].pb(i);
}
rep (X, 1, K) {
int anc = 0;
iter (&u, vec[X]) {
if (anc == 0) anc = u;
anc = LCA(anc, u);
}
Anc[X] = anc;
}
function<void(int, int)> dfs = [&] (int u, int p) {
if (u != Anc[a[u]]) {
// cout << a[u] <<" "<<a[p] <<" a\n";
adj[a[u]].pb(a[p]);
}
iter (&v, ke[u]) {
if (v == p) continue;
dfs(v, u);
}
};
dfs(1, 0);
stack<int> st;
function<void(int)> dfs2 = [&] (int u) {
static int time_dfs = 0;
num[u] = low[u] = ++time_dfs;
st.push(u);
iter (&v, adj[u]) {
if (del[v]) continue;
if (!num[v]) {
dfs2(v);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], num[v]);
}
if (low[u] == num[u]) {
int v;
++nSCC;
do {
v = st.top();
st.pop();
del[v] = 1;
SCC[v] = nSCC;
++sz[nSCC];
}while (v != u);
}
};
rep (i, 1, K) {
if (!num[i]) dfs2(i);
}
int res = K;
rep (u, 1, K) {
iter (&v, adj[u]) {
int cu = SCC[u], cv = SCC[v];
if (cv != cu) dp[cu] = 1;
}
}
rep (u, 1, nSCC) {
if (dp[u] == 0) {
res = min(res, sz[u] - 1);
}
}
cout << res <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +36
20 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
10
9
8
5
6
7
1
2
3
4
5
6
7
1
2
3
4
8
9
10
*/
# | 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... |