J. Circular Dance

J. Circular Dance

There are n kids, numbered from 11 to n, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as p1p1, p2p2, ..., pn (all these numbers are from 11 to n and are distinct, so pp is a permutation). Let the next kid for a kid pipi be kid pi+1pi+1 if i<n and p1p1 otherwise. After the dance, each kid remembered two kids: the next kid (let's call him xx) and the next kid for xx. Each kid told you which kids he/she remembered: the kid ii remembered kids ai,1 and ai,2. However, the order of ai,1 and ai,2 can differ from their order in the circle.

imgExample: 5 kids in a circle, p=[3,2,4,1,5]p=[3,2,4,1,5] (or any cyclic shift). The information kids remembered is: a1,1=3a1,1=3, a1,2=5a1,2=5; a2,1=1a2,1=1, a2,2=4a2,2=4; a3,1=2a3,1=2, a3,2=4a3,2=4; a4,1=1a4,1=1, a4,2=5a4,2=5; a5,1=2a5,1=2, a5,2=3a5,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 nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of the kids.

The next nn lines contain 22 integers each. The ii-th line contains two integers ai,1ai,1 and ai,2ai,2 (1≤ai,1,ai,2≤n,ai,1≠ai,21≤ai,1,ai,2≤n,ai,1≠ai,2) — the kids the ii-th kid remembered, given in arbitrary order.

Output

Print nn integers p1p1, p2p2, ..., pnpn — permutation of integers from 11 to nn, 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.

Examples

input

5
3 5
1 4
2 4
1 5
2 3

output

3 2 4 1 5 

input

3
2 3
3 1
1 2

output

3 1 2 

题目描述:

n个小盆友围成一圈,每个小盆友只记得后两个小盆友的编号,但不记得编号顺序。输入会按1~n的顺序,依次给出对应编号的人记得的下2个人。要求输出一种可能的小盆友的排列顺序。

分析:

因为一个人记得2个人,这3个人一定是连起来的,但是顺序不确定。比如m,y,x。m记得后面2个是y和x,我们要找出y和x的顺序。如果y在中间,就是y在x前面,那么y就一定认识x(因为y认识的后2个中,x是其中一个)。否则,x在y前面,顺序为m,x,y。

依次打印出来,用used记录是否打印过,最后接起来即可。

下面代码用pair来记录m认识的人,m.first是它认识的第一个人,m.second是第二个。

代码:

#include<iostream> 
#include<cmath>
#include<cstring>
#define MAX(x,y) x>y?x:y;
using namespace std;
pair <int,int> p[200006];
bool used[200006];
int kon(int m)
{
    int last;//排在末尾的小盆友 
    //按照输入的格式为m,y,x 
    int y=p[m].first;
    int x=p[m].second;
    //y认识的人a,b 
    int a=p[y].first;
    int b=p[y].second;
    //如果x是y认识的人,y就在中间位置 
    if(x==a||x==b)
    {
        if(!used[y])
        {
            printf("%d ",y);
            used[y]=true;
        }
        if(!used[x])
        {
            printf("%d ",x);
            //这里没有标记已经使用是因为在后面接起来的时候再标记 
        }
        last=x;
    }
    //否则x在中间 
    else
    {
        if(!used[x])
        {
            printf("%d ",x);
            used[x]=true;
        }
        if(!used[y])
        {
            printf("%d ",y);
        }
        last=y;
    }
    return last;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&p[i].first,&p[i].second);
     } 
    int tail=kon(1);
    while(!used[tail])
    {
        used[tail]=true;
        tail=kon(tail);
    }
    return 0;
}
 

posted on 2020-01-19 14:22  Aminers  阅读(163)  评论(0编辑  收藏  举报

导航