QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#989. 進入仙人掌圖

Statistics

給定一棵樹,請加入盡可能多的邊,使得結果圖成為仙人掌圖(cactus graph)。

仙人掌圖是指圖中每一條邊最多只屬於一個簡單環的圖。此圖不得包含自環(self-loop)或重邊(parallel edge)。

輸入格式

第一行包含一個整數 $N$,代表樹的大小($1 \le N \le 200\,000$)。

接下來的 $N - 1$ 行,每行包含兩個整數 $u$ 和 $v$($1 \le u, v \le N, u \neq v$),表示節點 $u$ 和 $v$ 之間有一條邊。保證輸入的圖是一棵樹。

輸出格式

第一行輸出 $K$,代表可以加入圖中的最大邊數。接下來的 $K$ 行,每行輸出兩個整數 $a$ 和 $b$($1 \le a, b \le N, a \neq b$),表示你要在節點 $a$ 和 $b$ 之間加入一條邊。結果圖必須是一個仙人掌圖。

若存在多種達到最大值 $K$ 的解,輸出其中任意一種即可。

範例

輸入 1

6
6 4
3 1
3 6
4 5
2 3

輸出 1

2
2 1
3 5

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.