Codeforces Round 529 (Div. 3) D. Circular Dance

Circular Dance

time limit per test: 3 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

There are n n n kids, numbered from 1 1 1 to n n n, dancing in a circle around the Christmas tree. Let’s enumerate them in a clockwise direction as p 1 p_1 p1, p 2 p_2 p2, …, p n p_n pn (all these numbers are from 1 1 1 to n n n and are distinct, so p p p is a permutation). Let the next kid for a kid p i p_i pi be kid p i + 1 p_{i + 1} pi+1 if i < n i < n i<n and p 1 p_1 p1 otherwise. After the dance, each kid remembered two kids: the next kid (let’s call him x x x) and the next kid for x x x. Each kid told you which kids he/she remembered: the kid i i i remembered kids a i , 1 a_{i, 1} ai,1 and a i , 2 a_{i, 2} ai,2. However, the order of a i , 1 a_{i, 1} ai,1 and a i , 2 a_{i, 2} ai,2 can differ from their order in the circle.

Example: 5 kids in a circle, p = [ 3 , 2 , 4 , 1 , 5 ] p=[3, 2, 4, 1, 5] p=[3,2,4,1,5] (or any cyclic shift). The information kids remembered is: a 1 , 1 = 3 a_{1,1}=3 a1,1=3, a 1 , 2 = 5 a_{1,2}=5 a1,2=5; a 2 , 1 = 1 a_{2,1}=1 a2,1=1, a 2 , 2 = 4 a_{2,2}=4 a2,2=4; a 3 , 1 = 2 a_{3,1}=2 a3,1=2, a 3 , 2 = 4 a_{3,2}=4 a3,2=4; a 4 , 1 = 1 a_{4,1}=1 a4,1=1, a 4 , 2 = 5 a_{4,2}=5 a4,2=5; a 5 , 1 = 2 a_{5,1}=2 a5,1=2, a 5 , 2 = 3 a_{5,2}=3 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.

Input

The first line of the input contains one integer n n n ( 3 ≤ n ≤ 2 ⋅ 1 0 5 3 \le n \le 2 \cdot 10^5 3n2105) — the number of the kids.

The next n n n lines contain 2 2 2 integers each. The i i i-th line contains two integers a i , 1 a_{i, 1} ai,1 and a i , 2 a_{i, 2} ai,2 ( 1 ≤ a i , 1 , a i , 2 ≤ n , a i , 1 ≠ a i , 2 1 \le a_{i, 1}, a_{i, 2} \le n, a_{i, 1} \ne a_{i, 2} 1ai,1,ai,2n,ai,1=ai,2) — the kids the i i i-th kid remembered, given in arbitrary order.

Output

Print n n n integers p 1 p_1 p1, p 2 p_2 p2, …, p n p_n pn — permutation of integers from 1 1 1 to n n 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.

Example

i n p u t \tt input input
5
3 5
1 4
2 4
1 5
2 3
o u t p u t \tt output output
3 2 4 1 5
i n p u t \tt input input
3
2 3
3 1
1 2
o u t p u t \tt output output
3 1 2

Tutorial

可以用两个变量 u u u v v v 标记下一个位置和下下个位置,若 u u u 孩子记住的孩子 v 1 , v 2 v_1, v_2 v1,v2 中有 v v v,则将 u u u 置为 v v v,将 v v v 置为另一个孩子(如果 v = v 1 v = v_1 v=v1,则将 v v v 置为 v 2 v_2 v2,如果 v = v 2 v = v_2 v=v2,则将 v v v 置为 v 1 v_1 v1),此时找到了一条边,就可以马上找下一条边,因此我们只需要确定整个图的一条边即可,由于从 1 1 1 出发的只有 2 2 2 条边,所以可以枚举从 1 1 1 出发的两条边,判断哪条边可以符合题意即可

此解法时间复杂度为 O ( n ) \mathcal O(n) O(n)

Solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector g(n + 1, vector<int>(2));
    for (int i = 1; i <= n; ++i) {
        cin >> g[i][0] >> g[i][1];
    }

    function<bool(int, int)> check = [&](int u, int v) -> bool {
        vector<int> ans;
        for (int i = 0; i < n; ++i) {
            if (g[u][0] == v) {
                ans.emplace_back(g[u][1]);
            } else if (g[u][1] == v) {
                ans.emplace_back(g[u][0]);
            } else {
                return false;
            }
            u = v;
            v = ans.back();
        }
        for (int ai : ans) {
            cout << ai << " \n"[ai == ans.back()];
        }
        return true;
    };
    
    for (int i = 0; i < 2; ++i) {
        if (check(1, g[1][i])) {
            return;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值