TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

One day, you woke up in a huge maze. You did not remember why you are here, but you do remember that you have an important exam this morning! Thus you would like to get out of this maze as soon as possible. You found a map of this maze on the wall, but unfortunately the map did not show where you are. You have to mark yourself on the map first.

According to the map, this maze is formed by rooms and passages. Each passage connects two rooms, and does not intersect with other passages. These passages may form cycles. Each passage belongs to at most 1 cycle, and each room belongs to at most 2 cycles. Also, a room is connected by no more than 2+k passages where k is the number of cycles that this room belongs to. Further more, the i-th cycle may have a common point with ai cycles and there are bi passages which do not belong to any cycle but connect to exactly 1 room in this cycle. ai + bi <= 2.

You walked around for a while, starting from a room and ending in another room. During your walk, you drew a local map of the rooms and passages that you just walked through. You are sure that if a passage on your local map is in a cycle, then all passages in that cycle are also on the local map.

Now, you have the map of whole maze as well as your local map. Can you write a program to determine all the possible locations that you are in?

Input Format

The first line of input contains an integer T, which is the number of test cases. Any two consecutive test cases are separated by a blank line.

Each test case consists of two map definitions. The former one is the map of the whole maze, and the latter one is the local map. Each map definition begins with two integers, N and M. N and M denote the number of rooms and passages on the map, respectively. Each of the next M lines contains two integers ai and bi, which represent a passage between room ai and room bi.

  • For the map of the whole maze, 1 <= N <= 100000.
  • For the local map, 1 <= N <= 1000.
  • N-1 <= M <= 2N
  • 1 <= ai, bi <= N
  • On the local map, room 1 is your starting room, and room N is your ending room. Other rooms are given in arbitrary order.
  • For any two rooms on the same map, there exist at least one path from one room to another.

Output Format

For each test case, please output 2 lines.
The first line contains a number A, which is the number of possible locations.
The next line contains A numbers in increasing order, which are the possible rooms where your walk ends.
These A numbers are separated by one space.

Sample Input 1

2

5 5
1 2
2 3
1 3
3 4
4 5
2 1
1 2

7 8
1 2
2 3
3 4
2 4
4 5
4 6
5 6
6 7
4 4
2 3
3 4
2 4
4 1

Sample Output 1

3
3 4 5
2
2 6

Hints

Problem Source

Migrated from old NTUJ.

Ferng

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 5000 262144 2048