QOJ.ac

QOJ

时间限制: 5 s 内存限制: 512 MB 总分: 100

#3026. 小虫子

统计

小虫生活在一棵树上。这棵树有 $n$ 个顶点(是一个连通的无向无环图),小虫占据了顶点 $a$ 和 $b$ 之间的整条路径。

小虫想移动到另一条路径——顶点 $c$ 和 $d$ 之间的路径,因为那里阳光更充足。已知路径 $a \leftrightarrow b$ 和 $c \leftrightarrow d$ 没有公共顶点。

为了改变在树上的位置,小虫可以进行一些移动,移动方式为:小虫的一端进入一个空闲的顶点。形式化地说,如果小虫当前占据 $x$ 和 $y$ 之间的路径,它可以选择一个与 $x$ 相邻且不在路径 $x \leftrightarrow y$ 上的新顶点 $z$。然后小虫释放(不再占据)$y$,转而占据 $z$。类似地,小虫可以选择一个与 $y$ 相邻的顶点 $z$,释放 $x$ 并占据 $z$。单次移动后,小虫仍然占据某条路径,且路径长度保持不变。

小虫的目标是到达 $c$ 和 $d$ 之间的路径,但由于它很懒,它计划的移动次数不超过 $10 \cdot n$ 次。你能帮助它在限制内达到目标吗?

输入格式

第一行包含测试用例的数量 $z$ ($1 \le z \le 7000$)。接下来是各个测试用例,格式如下:

每个测试用例的第一行包含一个整数 $n$ ($4 \le n \le 100\,000$),表示树的顶点数。 接下来的 $n - 1$ 行,每行包含两个整数 $u, v$ ($1 \le u \neq v \le n$),描述一条边的两个端点。 下一行给出两个整数 $a$ 和 $b$ ($1 \le a \neq b \le n$),这是小虫起始位置路径的两个端点。 下一行包含目标路径的两个端点,由两个整数 $c$ 和 $d$ ($1 \le c \neq d \le n$) 给出。 $a$ 和 $b$ 之间的路径上的顶点数与 $c$ 和 $d$ 之间的路径上的顶点数相同。你还可以假设这两条路径没有公共顶点。

所有测试用例的 $n$ 之和不超过 $1\,000\,000$。

输出格式

对于每个测试用例,如果小虫无法在 $10 \cdot n$ 次移动内到达目标,输出 $-1$。否则,分两行输出小虫可能的移动序列:第一行包含移动次数 $q$ ($1 \le q \le 10 \cdot n$),第二行包含 $q$ 个整数 $v_1, v_2, \dots, v_q$,表示所需的移动。对于 $i = 1, 2, \dots, q$,值 $v_i$ 应表示小虫在第 $i$ 次移动中进入的顶点。你可以输出任何正确的移动序列,只要它能将小虫移动到目标且移动次数不超过 $10 \cdot n$(特别地,你不必最小化移动次数)。假设小虫是对称的——它可以向两个方向移动,并且可以以任一端朝向目标路径进入目标路径。

样例

样例输入 1

3
6
1 2
1 3
1 4
4 5
4 6
2 3
5 6
15
1 2
1 6
2 3
2 4
2 5
6 7
6 8
5 9
6 10
9 11
9 12
9 13
12 14
14 15
14 13
3 6
6
1 2
1 3
2 4
4 5
5 6
4 6
3 2

样例输出 1

-1
7
15 5 2 1 6 7 3
3
2 1 3

说明

我们只考虑简单路径,即没有顶点出现两次的路径。众所周知,树中任意两个顶点之间都由唯一的一条简单路径连接。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.