#include #include #include #define INF 1000000000 using namespace std; int n, K, spec[310], oras[310], dist[310][310]; vector G[10100]; bool viz[310], permis[310][310]; int A[310], B[310]; void BFS(int ind) { queue Q; int i, x, best[10100]; vector ::iterator it; for(i = 1; i <= n; ++i) best[i] = INF; best[spec[ind]] = 0; Q.push(spec[ind]); while(!Q.empty()) { x = Q.front(); Q.pop(); for(it = G[x].begin(); it != G[x].end(); ++it) { if(best[*it] > best[x] + 1) { best[*it] = best[x] + 1; Q.push(*it); } } } for(i = 1; i <= K; ++i) dist[ind][i] = best[oras[i]]; } bool HopcroftKarp(int x) { if(viz[x]) return false; int i; viz[x] = true; for(i = 1; i <= K; ++i) { if(permis[x][i] && !A[i]) { A[i] = x; B[x] = i; return true; } } for(i = 1; i <= K; ++i) { if(permis[x][i] && HopcroftKarp(A[i])) { A[i] = x; B[x] = i; return true; } } return false; } bool Bun(int maxim) { int i, j; bool modif = true; for(i = 1; i <= K; ++i) A[i] = B[i] = 0; for(i = 1; i <= K; ++i) for(j = 1; j <= K; ++j) { if(dist[i][j] <= maxim) permis[i][j] = true; else permis[i][j] = false; } while(modif) { modif = false; for(i = 1; i <= K; ++i) viz[i] = false; for(i = 1; i <= K; ++i) if(!B[i]) modif |= HopcroftKarp(i); } for(i = 1; i <= K; ++i) if(!B[i]) return false; return true; } int BinarySearch() { int st, dr, mij, rez; st = 0; dr = rez = 2 * n; while(st <= dr) { mij = (st + dr) / 2; if(Bun(mij)) { rez = mij; dr = mij - 1; } else st = mij + 1; } return rez; } int main() { int i, x, y; cin >> n >> K; for(i = 1; i <= K; ++i) cin >> spec[i]; for(i = 1; i <= K; ++i) cin >> oras[i]; for(i = 1; i < n; ++i) { cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } for(i = 1; i <= K; ++i) BFS(i); cout << BinarySearch() << "\n"; return 0; }