QOJ.ac

QOJ

Límite de tiempo: 4 s Límite de memoria: 512 MB Puntuación total: 100

#16301. Counterintelligence

Estadísticas

In Byteotia, there are $n$ cities, numbered from $1$ to $n$, and $n-1$ roads, each directly connecting two cities. From every city, it is possible to reach every other city in exactly one way without backtracking.

You are in charge of the Byteotian counterintelligence. You have just received information that spies from hostile Bitotia have infiltrated some cities! You know that Bitotian spies always operate in pairs. When one spy from a pair discovers useful information, they attempt to reach the city where the second spy is located to share their findings. For each of the $q$ pairs of spies, you know exactly which cities the spies in that pair are currently in.

Your task is to ensure that no pair of spies can meet. To achieve this, you can declare a quarantine in any set of cities. It is forbidden to enter, pass through, or leave a city under quarantine.

Spies forming a pair can meet if and only if there exists a sequence of cities $x_1, x_2, \dots, x_k$, none of which have been placed under quarantine, such that $x_1$ is the city where one spy is located, $x_k$ is the city where the other spy is located, and for every $1 \le i \le k-1$, the cities $x_i$ and $x_{i+1}$ are directly connected by a road.

Naturally, you do not want to paralyze the entire country, so you want to place as few cities as possible under quarantine. Your task is to calculate the minimum number of cities that must be placed under quarantine to prevent every pair of spies from meeting. Additionally, you must provide any such shortest list of cities that must be placed under quarantine to achieve this.

Input

The first line of the input contains two integers $n$ and $q$ ($2 \le n \le 5 \cdot 10^5$, $1 \le q \le 5 \cdot 10^5$), representing the number of cities in Byteotia and the number of spy pairs, respectively.

The next $n-1$ lines contain the description of the roads. The $i$-th line (for $1 \le i \le n-1$) contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le n$, $a_i \neq b_i$), meaning that cities $a_i$ and $b_i$ are directly connected by a road.

The next $q$ lines contain the description of the spy pairs. The $i$-th line (for $1 \le i \le q$) contains two integers $c_i$ and $d_i$ ($1 \le c_i, d_i \le n$, $c_i \neq d_i$), representing the cities where the $i$-th pair of spies is located (one is in city $c_i$, and the other is in city $d_i$). Multiple spies (from different pairs) may be in the same city.

Output

The first line of the output should contain a single integer $s$, representing the minimum number of cities that must be placed under quarantine to prevent every pair of spies from meeting. The second line should contain $s$ integers representing the cities that must be placed under quarantine to achieve this.

Examples

Input 1

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

Output 1

2
2 3

Note

There are three pairs of spies, denoted in the figure by letters $A$, $B$, and $C$. If cities $2$ and $3$ (marked with circles) are placed under quarantine, no pair of spies can meet without visiting one of these cities. Other valid lists of cities to place under quarantine include, for example, $1$ and $3$, or $1$ and $7$.

Diagram of the spy network

Subtasks

Subtask Constraints Points
0a $n, q \le 20$ 9
0b $q = 1$ 5
0c $q = 2$ 6
0d $n \le 1000, q \le 1000$ 10
0e The graph is a line 10
1 Each path connecting a pair of spies intersects with at most one other path 17
2 $a_i = i, b_i = i + 1$ for $1 \le i \le n - 1$ 12
3 $a_i = i + 1, b_i = \lfloor \frac{i+1}{2} \rfloor$ for $1 \le i \le n - 1$ 23
4 No additional constraints 21

If only the first line of your answer is correct, your solution will receive 80% of the points for that test. You do not need to output the second line to receive these points.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.