#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define dbg(x) (cout<<#x<<" = "<<(x)<<'\n') #ifdef HOME const string inputFile = "input.txt"; const string outputFile = "output.txt"; #else const string problemName = ""; const string inputFile = problemName + ".in"; const string outputFile = problemName + ".out"; #endif typedef long long int lld; typedef pair PII; typedef pair PIL; typedef pair PLI; typedef pair PLL; const int INF = (1LL << 31) - 1; const lld LINF = (1LL << 62) - 1; const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; const int MOD = (int)(1e9) + 7; const int NMAX = 10000 + 5; const int KMAX = 300 + 5; int N, K, sol, destination, ok; vector V[NMAX]; int lvl[NMAX]; int A[KMAX]; int B[KMAX]; bitset viz; void dfs(int x) { if(x == destination) { ok = 1; return; } viz[x] = 1; for(auto y : V[x]) { if(viz[y]) continue; lvl[y] = lvl[x] + 1; dfs(y); if(ok) return; } } int main() { int i, x, y, z, p; freopen(inputFile.c_str(), "r", stdin); freopen(outputFile.c_str(), "w", stdout); scanf("%d%d", &N, &K); for(i = 1; i <= K; i++) scanf("%d", &A[i]); for(i = 1; i <= K; i++) scanf("%d", &B[i]); for(i = 2; i <= N; i++) { scanf("%d%d", &x, &y); V[x].push_back(y); V[y].push_back(x); } for(i = 1; i <= K; i++) { viz = 0; ok = 0; destination = B[i]; lvl[A[i]] = 0; dfs(A[i]); sol = max(sol, lvl[B[i]]); } printf("%d", sol); return 0; }