QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 256 MB Puntuación total: 100

#17136. OldPost -- NewPost

Estadísticas

在 Trilyandia 国,有 $n$ 个城市,由 $n - 1$ 条道路连接。该国的道路系统非常特殊:

  • 一条道路仅连接两个不同的城市;
  • 任意两个城市之间最多只有一条道路;
  • 从任意一个城市出发,都可以到达其他任何城市(可能需要途径其他城市)。

邮政公司 OldPost 决定在该国提供快递服务。由于员工人数有限,运输仅沿着一条长度为 $l$ 的路线 $a$ 进行,即一个城市序列 $a_1, a_2, \dots, a_l$,满足:

  • 对于 $i \neq j$,有 $a_i \neq a_j$;
  • 城市 $a_i$ 和 $a_{i+1}$ 之间有道路相连($1 \le i < l$)。

为了最大化利润,OldPost 决定沿着具有最大长度的路线运营。

OldPost 的老竞争对手 NewPost 决定做同样的事情,但沿着它自己的路线 $b$:$b_1, b_2, \dots, b_l$。路线 $b$ 不一定与 $a$ 不同,但也必须具有最大可能的长度。

总统在得知两家公司的意图后,要求 OldPost 和 NewPost 选择路线 $a$ 和 $b$,以便覆盖尽可能多的城市(即,确保在至少一条路线中的城市数量最大化)。由于 Trilyandia 的总统擅长管理,而邮政公司擅长寄送物品而非寻找路线,因此你被委以重任,来寻找这样的 $a$ 和 $b$。

请注意,每家公司都希望其路线具有最大可能的长度;即使选择较短的路线可以增加覆盖的城市总数,它们也不会妥协。

请帮助寻找具有最大可能长度且覆盖最多城市数量的路线 $a$ 和 $b$。

输入格式

第一行包含一个整数 $n$($2 \le n \le 6 \cdot 10^5$)—— 该国的城市数量。

接下来的 $n - 1$ 行,每行包含两个整数 $x_i$ 和 $y_i$($1 \le x_i, y_i \le n, x_i \neq y_i$)—— 表示有一条道路连接的城市对。

输出格式

第一行应包含一个整数 $l$($2 \le l \le n$)—— 找到的路线长度。

第二行应包含 $l$ 个整数 $a_1, a_2, \dots, a_l$($1 \le a_i \le n$)—— 路线 $a$ 中的城市。

第三行应包含 $l$ 个整数 $b_1, b_2, \dots, b_l$($1 \le b_i \le n$)—— 路线 $b$ 中的城市。

如果有多种最优路线,输出其中任意一种即可。

子任务

  1. (8 分):$n \le 10$;
  2. (8 分):$n \le 20$;
  3. (6 分):$n \le 1\,000$;存在恰好一个城市与所有其他城市直接相连;
  4. (8 分):$n \le 20\,000$;存在恰好两个城市仅与一个其他城市相连(城市排成一条链);
  5. (8 分):$n \le 20\,000$;存在恰好一个城市与三个或更多城市相连;
  6. (8 分):$n \le 10\,000$;不超过 10 个城市仅与一个其他城市相连;
  7. (16 分):$n \le 1\,000$;
  8. (18 分):$n \le 70\,000$;
  9. (20 分):无附加限制。

样例

输入样例 1

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

输出样例 1

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

说明

在样例中,最长可能路线如下:

  1. 5-4-3-2-6-7
  2. 9-8-3-2-6-7
  3. 5-4-3-2-1-10
  4. 9-8-3-2-1-10

第一条和第四条路线可以覆盖所有城市。

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.