QOJ.ac

QOJ

Limite de temps : 5 s Limite de mémoire : 1024 MB Points totaux : 100

#4289. 高效公交路线规划

Statistiques

在 Optopia 这个国家,他们凡事都追求极致的优化,Optopia 内部的一切设计也都以此为目标。例如,为了保证 GPS 路线规划总是最优的,Optopia 的道路网络被设计成任意两个城市之间有且仅有一条路径。

SL bus line 4. Wikimedia Commons, CC BY-SA 4.0.

Optopia 还有一套公交线路网络,每条公交线路在两个城市之间往返,并停靠沿途的所有城市。最近,Optopia 发生了一起大丑闻。一位 Optopia 公民发现,目前的公交线路网络实际上并非最优。它使用的公交线路太多了!

这就是你登场的时候了。你被 Optopia 聘请来修复他们这个令人尴尬的错误。你的任务是设计一个新的最优公交线路网络。Optopia 对新的公交线路网络提出了两点要求:

  • 必须能够通过公交车在任意两个城市之间往返。
  • 公交线路的数量应尽可能少。

将 Optopia 这个国家建模为一个无向图,其中节点代表 Optopia 的城市,边代表城市之间的道路。此外,用两个城市来表示每条公交线路,即该线路的两个终点站。

输入格式

第一行包含一个整数 $N$ ($3 \le N \le 2 \cdot 10^5$),表示 Optopia 的城市数量。接下来的 $N - 1$ 行,每行包含两个整数 $u$ 和 $v$ ($1 \le u, v \le N, u \neq v$),表示一对由道路连接的城市。保证 Optopia 的道路网络是连通的,即可以通过道路网络在任意两个城市之间往返。

输出格式

第一行输出最优的公交线路数量 $M$。接下来输出 $M$ 行,其中第 $i$ 行应包含两个空格分隔的整数 $a_i, b_i$ ($1 \le a_i, b_i \le N$),表示第 $i$ 条公交线路的两个端点。

样例

输入格式 1

3
1 2
2 3

输出格式 1

1
1 3

输入格式 2

5
1 2
1 3
1 4
1 5

输出格式 2

2
2 4
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.