并查集 合并序列

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

F. Asya And Kittens

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.

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

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.

这个并查集用的很巧妙。 通过数组的记录实现链状的并查集。这也为我们维护链表提供了一种思路,我们可以用一个数组fa[],让fa[i]指向i节点所在链表的头结点。在开一个low[],记录i节点所在链表的最后一个位置。

#include<bits/stdc++.h>
using namespace std;
const int maxn=15e4+5;
int N,x,y,fa[maxn],son[maxn],low[maxn];
//fa[i]表示的还是i节点最根的父亲
//son[i]表示的是i节点的直接孩子
//low[i]表示的是当前节点所在链最低层的节点的编号
int find(int x){
	return (x==fa[x]?x:fa[x]=find(fa[x]));
}
int main()
{
	scanf("%d",&N);
	for(int i=1;i<=N;i++) fa[i]=i,low[i]=i;
	for(int i=0;i<N-1;i++){
		scanf("%d%d",&x,&y);
		x=find(x),y=find(y);
		fa[y]=x;
		son[low[x]]=y;
		low[x]=low[y];
	}	
	for(int i=find(1);i;i=son[i]){
		printf("%d ",i);
	}
	return 0;
} 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值