LTCS
题号:NC239321
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Tcs has just learned the subsequence of a sequence. Now Tcs wants to define the subsequence of a rooted tree. A rooted tree T_a which satisfies the following constraints is called a subsequence of another rooted tree T_b:
  1. Suppose that there are N vertices in T_a numbered as and there are M vertices in T_b numbered as . There is a function , which satisfies .
  2. For all vertices u in T_a holds, where  is the value of vertex u in T_i.
  3. If vertex u is an ancestor of vertex v in T_a, then vertex F(u) is an ancestor of vertex F(v) in T_b.
  4. For distinct vertices u, v, w in T_a, if vertex u is the father of both vertex v and vertex w, then vertex F(u) is on the simple path between vertex F(v) and vertex F(w) in T_b.

A rooted tree T_3 is called a TCS (Tree Common Subsequence) of T_1 and T_2 when T_3 is a subsequence of both rooted trees T_1 and T_2. Given the rooted trees T_1 and T_2 whose roots are both vertex 1, Tcs wants to find the largest size of the TCS of T_1 and T_2.

输入描述:

The first line contains two integers n, m () — the number of vertices in T_1 and T_2, respectively.

The second line contains n integers () — the value of each vertex in T_1.

The third line contains m integers () — the value of each vertex in T_2.

Each of the next n-1 lines contains two integers u and v () — an edge connecting vertices u and v in T_1.

Each of the next m-1 lines contains two integers u and v () — an edge connecting vertices u and v in T_2.

输出描述:

Print an integer in a single line, denoting the largest size of the TCS of T_1 and T_2.
示例1

输入

复制
3 3
1 1 1
1 1 1
1 2
1 3
1 2
2 3

输出

复制
2
示例2

输入

复制
3 3
1 2 3
2 2 3
1 2
1 3
1 2
2 3

输出

复制
1