Anti-Plagiarism
Time limit5sMemory limit2048 MB
For each pair of trees, decide whether the second tree is a subgraph of the first, meaning the first can be obtained by extending the second.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
As homework, a teacher asked every student in an art class to draw a beautiful and, above all, original tree. After the submissions, she began to suspect some students of cheating. The teacher considers a tree to be copied from a tree if it is possible to add some (possibly zero) vertices and edges to and relabel its vertices so that it becomes the same as . She suspects pairs of students. For each pair of trees, check whether the first tree could be copied from the second tree.
Input
The first line contains an integer (), the number of suspicious pairs. Each description of a pair begins with an integer (). The next lines each contain two integers and (), the edges of the first student's tree. The next line contains an integer (). The next lines each contain two integers and (), the edges of the second student's tree. The sum of over all pairs does not exceed , and the sum of does not exceed .
Output
For each pair, print a line containing a single word, "Yes" if the first tree could be copied from the second tree, or "No" otherwise. The word is case-insensitive.