codeforces 1131 F. Asya And Kittens

F. Asya And Kittens

http://codeforces.com/contest/1131/problem/F

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Asya loves animals very much. Recently, she purchased nn kittens, enumerated them from 11 and nn and then put them into the cage. The cage consists of one row of nn cells, enumerated with integers from 11 to nn from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n−1n−1 partitions originally. Initially, each cell contained exactly one kitten with some number.

Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day ii , Asya:

  • Noticed, that the kittens xixi and yiyi , located in neighboring cells want to play together.
  • Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells.

Since Asya has never putted partitions back, after n−1n−1 days the cage contained a single cell, having all kittens.

For every day, Asya remembers numbers of kittens xixi and yiyi , who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into nn cells.

Input

The first line contains a single integer nn (2≤n≤1500002≤n≤150000 ) — the number of kittens.

Each of the following n−1n−1 lines contains integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n , xi≠yixi≠yi ) — indices of kittens, which got together due to the border removal on the corresponding day.

It's guaranteed, that the kittens xixi and yiyi were in the different cells before this day.

Output

For every cell from 11 to nn print a single integer — the index of the kitten from 11 to nn , who was originally in it.

All printed integers must be distinct.

It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.

Example

Input

Copy

5
1 4
2 5
3 1
4 5

Output

Copy

3 1 4 2 5

Note

The answer for the example contains one of several possible initial arrangements of the kittens.

The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells.

 一开始暴力又是MLE又是TLE,判断一下交换的大小就过了,把小的接到大的后面

 

#include<bits/stdc++.h>
using namespace std;
queue<int>q[150005];
int father[150005];
int len[150005];
int fin(int x)
{
    return father[x]=father[x]==x?x:fin(father[x]);
}

void init(int n)
{
    for(int i=1; i<=n; i++)
    {
        father[i]=i;
        q[i].push(i);
        len[i]=1;
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    init(n);
    for(int cas=1; cas<n; cas++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        int a=fin(x);
        int b=fin(y);
        if(len[a]<len[b])
        {
           swap(a,b);
        }
        father[b]=a;
        len[a]=len[a]+len[b];
        len[b]=0;
        while(!q[b].empty())
        {
            q[a].push(q[b].front());
            q[b].pop();
        }
    }
    int u=fin(1);
    while(!q[u].empty())
    {
        printf("%d ",q[u].front());
        q[u].pop();
    }
}

 并查集版本   nxt是一个元素的下一个,last是一个元素的上一个

l是这个元素的父节点所在集合的最左边的,r是右边的

#include<bits/stdc++.h>
using namespace std;
const int maxn=150005;

int father[maxn];
int nxt[maxn];
int last[maxn];
int l[maxn];
int r[maxn];
void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        father[i]=i;
        nxt[i]=-1;
        last[i]=-1;
        l[i]=i;
        r[i]=i;
    }
}
int fin(int x)
{
    return father[x]=father[x]==x?x:fin(father[x]);
}
void merg(int x,int y)
{
    int a=fin(x);
    int b=fin(y);
    father[b]=a;
    nxt[r[a]]=l[b];//拼接
    last[l[b]]=r[a];
    r[a]=r[b];
}
int main()
{
    int n;
    scanf("%d",&n);
    init(n);
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        merg(x,y);
    }
    int s;
    for(int i=1;i<=n;i++)
    {
        if(last[i]==-1)
        {
            s=i;
            break;
        }
    }
    for(int i=s;i!=-1;i=nxt[i])
    {
        printf("%d ",i);
    }
}

 

全部评论

相关推荐

数开小菜鸡_暂退沉淀版:大二第三段,还是字节,这下真得点点举办了
点赞 评论 收藏
分享
牛客33727151号:不是哥们我以为驾照是段子呢
点赞 评论 收藏
分享
用户64975461947315:这不很正常吗,2个月开实习证明,这个薪资也还算合理,深圳Java好多150不包吃不包住呢,而且也提前和你说了没有转正机会,现在贼多牛马公司骗你说毕业转正,你辛辛苦苦干了半年拿到毕业证,后面和你说没hc了😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务