#H2026M. Madoka and Homura
Madoka and Homura
Description
You are given two rooted trees and . The root of each tree is vertex .
Determine whether there exists a non-empty set of vertices such that the virtual tree of in is isomorphic to as a rooted tree.
The isomorphism only needs to preserve the parent-child structure. It does not need to preserve vertex labels.
Note that the root of may correspond to any vertex of ; it does not have to correspond to the root of .
The virtual tree is formally defined as follows.
For a non-empty set of vertices in , let its LCA closure be the smallest set satisfying all of the following conditions:
- every vertex in belongs to ;
- for every , the vertex in also belongs to .
Construct a rooted tree on the vertices of . Its root is the vertex of minimum depth in . For every other vertex , its parent is the deepest strict ancestor of that belongs to . The resulting tree is called the virtual tree of in .
Input
The first line contains one integer (), the number of vertices in .
Each of the next lines contains two integers and (), denoting an undirected edge between vertices and in .
The next line contains one integer (), the number of vertices in .
Each of the next lines contains two integers and (), denoting an undirected edge between vertices and in .
It is guaranteed that both given graphs are trees. Both trees are rooted at vertex .
Output
Print if can be obtained as a virtual tree of . Otherwise, print .
Examples
Input
5
1 2
2 3
3 4
3 5
3
1 2
1 3
Output
YES
Input
5
1 2
2 3
3 4
4 5
3
1 2
1 3
Output
NO
Note
In the first example, choose . Its LCA closure is , and the resulting virtual tree is isomorphic to .
In the second example, every virtual tree of a rooted path is also a rooted path, so it cannot be isomorphic to .
相关
在下列比赛中: