QOJ.ac

QOJ

حد الوقت: 4 s حد الذاكرة: 512 MB مجموع النقاط: 100

#16301. 反情報

الإحصائيات

在 Byteotia 有 $n$ 個城市,編號從 $1$ 到 $n$,以及 $n-1$ 條道路,每條道路直接連接兩個城市。從任何一個城市出發,都可以且僅能透過唯一一條路徑到達其他所有城市,且路徑中不會經過重複的城市。

你負責 Byteotia 的反間諜工作。你剛收到情報,敵對勢力 Bitotia 的間諜已經滲透進了一些城市!你知道 Bitotian 間諜總是成對行動。當一對間諜中的其中一人發現有用的情報時,他們會嘗試前往另一名間諜所在的城市以分享發現。對於 $q$ 對間諜中的每一對,你都知道這對間諜目前分別位於哪兩個城市。

你的任務是確保沒有任何一對間諜能夠會合。為了達成這個目標,你可以宣佈對任意一組城市進行隔離。禁止進入、通過或離開被隔離的城市。

一對間諜能夠會合,若且唯若存在一個城市序列 $x_1, x_2, \dots, x_k$,其中沒有任何一個城市被隔離,使得 $x_1$ 是其中一名間諜所在的城市,$x_k$ 是另一名間諜所在的城市,且對於每個 $1 \le i \le k-1$,城市 $x_i$ 和 $x_{i+1}$ 之間有道路直接相連。

當然,你不想讓整個國家陷入癱瘓,因此你希望盡可能減少被隔離的城市數量。你的任務是計算為了防止每一對間諜會合,必須隔離的最少城市數量。此外,你必須提供任何一組符合該數量的城市列表。

輸入格式

輸入的第一行包含兩個整數 $n$ 和 $q$ ($2 \le n \le 5 \cdot 10^5$, $1 \le q \le 5 \cdot 10^5$),分別代表 Byteotia 的城市數量和間諜對的數量。

接下來的 $n-1$ 行包含道路的描述。第 $i$ 行(對於 $1 \le i \le n-1$)包含兩個整數 $a_i$ 和 $b_i$ ($1 \le a_i, b_i \le n$, $a_i \neq b_i$),表示城市 $a_i$ 和 $b_i$ 之間有道路直接相連。

接下來的 $q$ 行包含間諜對的描述。第 $i$ 行(對於 $1 \le i \le q$)包含兩個整數 $c_i$ 和 $d_i$ ($1 \le c_i, d_i \le n$, $c_i \neq d_i$),代表第 $i$ 對間諜所在的城市(一人在城市 $c_i$,另一人在城市 $d_i$)。多名間諜(來自不同對)可能位於同一個城市。

輸出格式

輸出的第一行應包含一個整數 $s$,代表為了防止每一對間諜會合,必須隔離的最少城市數量。第二行應包含 $s$ 個整數,代表為了達成此目標而必須隔離的城市。

範例

輸入 1

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

輸出 1

2
2 3

說明

圖中有三對間諜,分別以字母 $A$、$B$ 和 $C$ 表示。如果城市 $2$ 和 $3$(以圓圈標記)被隔離,則沒有任何一對間諜可以在不經過這些城市的情況下會合。其他有效的隔離城市列表還包括,例如,$1$ 和 $3$,或是 $1$ 和 $7$。

子任務

子任務 資料範圍 分數
1 $n, q \le 20$ 9
2 $q \le 2$ 11
3 每對間諜的路徑最多與另一條路徑相交一次 17
4 $a_i = i, b_i = i + 1$ 對於 $1 \le i \le n - 1$ 12
5 $a_i = i + 1, b_i = \lfloor \frac{i+1}{2} \rfloor$ 對於 $1 \le i \le n - 1$ 23
6 無額外限制 21

如果你的答案僅第一行正確,你的解法將獲得該測試點 80% 的分數。你不需要輸出第二行即可獲得這些分數。

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.