CodeForces1131 F. Asya And Kittens 并查集

题目连接:点击查看

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 xi

and yi

  • , 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, xi≠yi

) — 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

Copy

5
1 4
2 5
3 1
4 5

Output

Copy

3 1 4 2 5

题意:给你n-1对,相邻的在一块,选了这两个数,这两个数就可以看成一个整体了,问符合条件的序列。

题解:并查集啊,记录每个数在这一组中最前和最后的数,然后依次连接即可,再开两个数组记录下每个数前后的数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=150010;
int pre[N],nex[N];
int pr[N],ne[N];
int n;
int nextt(int x)
{
	return ne[x]==-1?x:ne[x]=nextt(ne[x]);
}
int pree(int x)
{
	return pr[x]==-1?x:pr[x]=pree(pr[x]);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		pre[i]=nex[i]=-1;
		pr[i]=ne[i]=-1;
	}
	int x,y;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		int xx=nextt(x);
		int yy=pree(y);
	//	cout<<xx<<" "<<yy<<endl;
		ne[xx]=yy;
		pr[yy]=xx;
		
		pre[yy]=xx;
		nex[xx]=yy;
	//	cout<<i<<endl;
	}
	int pos;
	for(int i=1;i<=n;i++)
	{
	//	cout<<nex[i]<<" "<<pre[i]<<endl;
		if(nex[i]!=-1&&pre[i]==-1)
		{
			pos=i;
			break;
		}
	}
	printf("%d",pos);
	pos=nex[pos];
	while(pos!=-1)
	{
		printf(" %d",pos);
		pos=nex[pos];
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/87897704