8674: LTCS

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:3 通过:1

题目描述


Tcs has just learned the subsequence of a sequence. Now Tcs wants to define the subsequence of a rooted tree. A rooted tree TaT_aTa which satisfies the following constraints is called a subsequence of another rooted tree TbT_bTb:
  1. Suppose that there are NNN vertices in TaT_aTa numbered as 1,2,…,N1,2,\ldots,N1,2,,N and there are MMM vertices in TbT_bTb numbered as 1,2,…,M1,2,\ldots,M1,2,,M. There is a function F:{1,2,…,N}→{1,2,…,M}F:\{1,2,\ldots,N\} \to \{1,2,\ldots,M\}F:{1,2,,N}{1,2,,M}, which satisfies ∀x,y∈{1,2,…,N},x≠y⇒F(x)≠F(y)\forall x, y \in \{1,2,\ldots,N\}, x \ne y \Rightarrow F(x) \not = F(y)x,y{1,2,,N},x=yF(x)=F(y).
  2. For all vertices uuu in TaT_aTaca,u=cb,F(u)c_{a, u} = c_{b, F(u)}ca,u=cb,F(u) holds, where ci,uc_{i, u}ci,u is the value of vertex uuu in TiT_iTi.
  3. If vertex uuu is an ancestor of vertex vvv in TaT_aTa, then vertex F(u)F(u)F(u) is an ancestor of vertex F(v)F(v)F(v) in TbT_bTb.
  4. For distinct vertices u,v,wu, v, wu,v,w in TaT_aTa, if vertex uuu is the father of both vertex vvv and vertex www, then vertex F(u)F(u)F(u) is on the simple path between vertex F(v)F(v)F(v) and vertex F(w)F(w)F(w) in TbT_bTb.

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

输入格式


The first line contains two integers n,mn, mn,m (2≤n,m≤5002\leq n, m\leq 5002n,m500) — the number of vertices in T1T_1T1 and T2T_2T2, respectively.
The second line contains nnn integers c1,1,c1,2,…,c1,nc_{1, 1}, c_{1, 2}, \dots, c_{1, n}c1,1,c1,2,,c1,n (1≤c1,i≤5001 \le c_{1, i} \le 5001c1,i500) — the value of each vertex in T1T_1T1.
The third line contains mmm integers c2,1,c2,2,…,c2,mc_{2, 1}, c_{2, 2}, \dots, c_{2, m}c2,1,c2,2,,c2,m (1≤c2,i≤5001 \le c_{2, i} \le 5001c2,i500) — the value of each vertex in T2T_2T2.
Each of the next n−1n-1n1 lines contains two integers uuu and vvv (1≤u,v≤n1 \le u, v \le n1u,vn) — an edge connecting vertices uuu and vvv in T1T_1T1. 
Each of the next m−1m-1m1 lines contains two integers uuu and vvv (1≤u,v≤m1 \le u, v \le m1u,vm) — an edge connecting vertices uuu and vvv in T2T_2T2.

输出格式


Print an integer in a single line, denoting the largest size of the TCS of 

输入样例 复制

3 3
1 1 1
1 1 1
1 2
1 3
1 2
2 3

输出样例 复制

2

数据范围与提示


分类标签