CF1095D.Circular Dance
普及/提高-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n kids, numbered from 1 to n, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as p1, p2, ..., pn (all these numbers are from 1 to n and are distinct, so p is a permutation). Let the next kid for a kid pi be kid pi+1 if i<n and p1 otherwise. After the dance, each kid remembered two kids: the next kid (let's call him x) and the next kid for x. Each kid told you which kids he/she remembered: the kid i remembered kids ai,1 and ai,2. However, the order of ai,1 and ai,2 can differ from their order in the circle.
Example: 5 kids in a circle, p=[3,2,4,1,5] (or any cyclic shift). The information kids remembered is: a1,1=3, a1,2=5; a2,1=1, a2,2=4; a3,1=2, a3,2=4; a4,1=1, a4,2=5; a5,1=2, a5,2=3.
You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
有 n 个孩子,编号为 1 到 n,正围绕圣诞树顺时针围成一个圆圈跳舞。我们按顺时针方向将他们依次记为 p1, p2, ..., pn(这些数均属于 1 到 n 且互不相同,因此 p 是一个排列)。对于孩子 pi,其“下一个孩子”定义为:若 i<n,则为 pi+1;否则(即 i=n)为 p1。舞蹈结束后,每个孩子都记住了两个孩子:一个是自己的“下一个孩子”(记作 x),另一个是 x 的“下一个孩子”。每个孩子告诉你他/她所记住的两个孩子:孩子 i 记住的孩子为 ai,1 和 ai,2。但注意,ai,1 和 ai,2 的顺序可能与他们在圆圈中的实际顺序不同。
示例:5 个孩子围成一圈,p=[3,2,4,1,5](或其任意循环移位)。孩子们记住的信息为:a1,1=3, a1,2=5;a2,1=1, a2,2=4;a3,1=2, a3,2=4;a4,1=1, a4,2=5;a5,1=2, a5,2=3。
你需要根据这些信息还原孩子们在圆圈中的顺序。若存在多个合法答案,输出任意一个即可。题目保证至少存在一个解。
如果你使用 Python 编程,请在提交代码时考虑使用 PyPy 而非 Python。
输入格式
The first line of the input contains one integer n (3≤n≤2⋅105) — the number of the kids.
The next n lines contain 2 integers each. The i-th line contains two integers ai,1 and ai,2 (1≤ai,1,ai,2≤n,ai,1=ai,2) — the kids the i-th kid remembered, given in arbitrary order.
输入的第一行包含一个整数 n(3≤n≤2⋅105)—— 小孩的数量。
接下来的 n 行每行包含两个整数。第 i 行包含两个整数 ai,1 和 ai,2(1≤ai,1,ai,2≤n,且 ai,1=ai,2)—— 表示第 i 个小孩所记住的另外两个小孩的编号,顺序任意。
输出格式
Print n integers p1, p2, ..., pn — permutation of integers from 1 to n, which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists.
输出 n 个整数 p1, p2, ..., pn —— 即 1 到 n 的一个排列,表示孩子们在圆圈中的顺序。若存在多个答案,可输出任意一个(例如,圆圈中谁排第一并不重要)。题目保证至少存在一个解。
输入输出样例
输入#1
5 3 5 1 4 2 4 1 5 2 3
输出#1
3 2 4 1 5
输入#2
3 2 3 3 1 1 2
输出#2
3 1 2
输入解题思路,AI测评打分。不知道怎么写?