Codeforces Round #541 (Div.2) 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 n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n−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 i, Asya:

  • Noticed, that the kittens x i and y i , 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−1 days the cage contained a single cell, having all kittens.

For every day, Asya remembers numbers of kittens xi and yi, 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 n cells.

Input

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

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

It’s guaranteed, that the kittens xi and yi were in the different cells before this day.

Output

For every cell from 1 to n print a single integer — the index of the kitten from 1 to n, 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

显然,贪心地把笼子按相反的顺序链接在一起就可以了。这里用并查集维护笼子是否在一起,并加入一些参数存储当前并查集内元素的顺序。

#include<bits/stdc++.h>
using namespace std;
struct dongcidaci
{
	int fa,son,ultson;//son是当前下一个节点,ultson是当前链上最后一个节点
}a[150004];
int mergefa(int now)
{
	if (a[now].fa==now) return now;
	a[now].fa=mergefa(a[now].fa);
	return a[now].fa;
}
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		a[i].fa=i;
		a[i].son=i;
		a[i].ultson=i;
	}
	int tx,ty;
	for(int i=0;i<n-1;i++)
	{
		cin>>tx>>ty;
		a[tx].fa=mergefa(tx);
		a[ty].fa=mergefa(ty);
        //两条链首尾相接,并维护并查集
		a[a[a[tx].fa].ultson].son=a[ty].fa;
		a[a[tx].fa].ultson=a[a[ty].fa].ultson;
		a[a[ty].fa].fa=a[tx].fa;
	}
	int now=mergefa(1);//不一定能从1开始输出,所以找1的链首开始
	while(a[now].son!=now)
	{
		cout<<now<<' ';
		now=a[now].son;
	}
	cout<<now<<endl;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值