#P13477. [GCJ 2008 APAC SemiFinal] Modern Art Plagiarism

[GCJ 2008 APAC SemiFinal] Modern Art Plagiarism

题目描述

You have pictures of two sculptures. The sculptures consist of several solid metal spheres, and some rubber pipes connecting pairs of spheres. The pipes in each sculpture are connected in such a way that for any pair of spheres, there is exactly one path following a series of pipes (without repeating any) between those two spheres. All the spheres have the same radius, and all the pipes have the same length.

You suspect that the smaller of the two sculptures was actually created by simply removing some spheres and pipes from the larger one. You want to write a program to test if this is possible.

The input will contain several test cases. One sculpture is described by numbering the spheres consecutively from 11, and listing the pairs of spheres which are connected by pipes. The numbering is chosen independently for each sculpture.

输入格式

  • One line containing an integer CC, the number of test cases in the input file.

For each test case, there will be:

  • One line containing the integer NN, the number of spheres in the large sculpture.
  • N1N-1 lines, each containing a pair of space-separated integers, indicating that the two spheres with those numbers in the large sculpture are connected by a pipe.
  • One line containing the integer MM, the number of spheres in the small sculpture.
  • M1M-1 lines, each containing a pair of space-separated integers, indicating that the two spheres with those numbers in the small sculpture are connected by a pipe.

输出格式

  • CC lines, one for each test case in the order they occur in the input file, containing "Case #XX: YES" if the small sculpture in case XX could have been created from the large sculpture in case XX, or "Case #XX: NO" if it could not. (XX is the number of the test case, between 11 and CC.)
2
5
1 2
2 3
3 4
4 5
4
1 2
1 3
1 4
5
1 2
1 3
1 4
4 5
4
1 2
2 3
3 4
Case #1: NO
Case #2: YES

提示

Sample Explanation

In the first case, the large sculpture has five spheres connected in a line, and the small sculpture has one sphere that has three other spheres connected to it. There's no way the smaller sculpture could have been made by removing things from the larger one.

In the second case, the small sculpture is four spheres connected in a line. These can match the larger sculpture's spheres in the order 21452-1-4-5.

Limits

Small dataset (7 Pts, Test set 1 - Visible)

  • 1C1001 \leq C \leq 100
  • 2N82 \leq N \leq 8
  • 1M<N1 \leq M < N

Large dataset (25 Pts, Test set 2 - Hidden)

  • 1C501 \leq C \leq 50
  • 2N1002 \leq N \leq 100
  • 1M<N1 \leq M < N