Asya And Kittens(并查集)

题目链接:传送门

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

5
1 4
2 5
3 1
4 5

output

3 1 4 2 5

题意描述:

       Asaya非常喜欢动物。他把n只猫(编号从1—n)放进n个笼子里,相邻的笼子由一块透明玻璃板分开。Asaya发现相邻的笼子的猫想一起玩,所以每一天,Asaya就去掉一块隔板,n-1天后,就变成一个笼子了。但Asaya忘记了原本猫的排序的了,只记得是哪两只猫想一起玩。输出猫原本的排序。

题目分析:

       先开个大的vecter<int>Q[maxn],里面先放它本身,然后用并查集处理u,v.把数组小的里面的元素按顺序放进另一个数组里面(不这样做会mle),得到的数组就是结果了。

代码:

#include<bits/stdc++.h>
using namespace std;
int n, fa[150005];
vector<int>Q[150005];
int found(int x){
    return x == fa[x]? x:fa[x] = found(fa[x]);
}
void unite(int x, int y){
    x = found(x), y = found(y);
    fa[x] = y;
}
int main(){
    cin >> n;
    for(int i=0;i<=n;++i){
        fa[i] = i;
    }
    for(int i=1;i<=n;++i){
        Q[i].push_back(i);
    }
    int u, v;
    int ans = 0;
    for(int i=1;i<n;++i){
        scanf("%d %d", &u, &v);
        u = found(u), v = found(v);
        if(Q[u].size() < Q[v].size()) swap(u, v);
        for(int j=0;j<Q[v].size();++j){
            Q[u].push_back(Q[v][j]);
        }
        Q[v].clear();
        fa[v] = u;
        ans = u;
    }
    ans = found(1);
    for(int i=0;i<Q[ans].size();++i){
        printf("%d", Q[ans][i]);
        i == n-1? printf("\n"):printf(" ");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值