CodeForces - 1131F Asya And Kittens 【双向并查集】

ps  双向并查集这个name是我自己yy出来的

Decription

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 nncells, 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−1partitions 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

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.

思路:

l[ i ],r[ i ]数组记录i左边第一个和右边第一个数,初始化为本身。全部排好后找到左边第一个(l等于本身),然后依此输出右边的每一个就ok了。

注意要开一个fl[ i] 和 fr[ i ] 数组记录i最左边的数和最右边的数 ,这俩数组用于路径压缩,不进行路径压缩会t( 仔细想想这题getf的时间复杂度很可能会接近 O(n))。

ACCode

#include <iostream>
using namespace std;
const int maxn = 1e5+5e4+5;
int l[maxn],r[maxn];	// 左边右边第一个 
int fl[maxn],fr[maxn];	// 左边右边最后一个 用于路径压缩 
void init(int n)
{
	for(int i=1;i<=n;i++) {
		l[i] = i;
		r[i] = i;
		fl[i] = i;
		fr[i] = i;
	}
} 
int getfl(int x)
{
	if(fl[x] == x)	return x;
	else return fl[x] = getfl(fl[x]);
}
int getfr(int x)
{
	if(fr[x] == x)	return fr[x];
	else return fr[x] = getfr(fr[x]);
}
void Merge(int x,int y)
{
	int rx = getfr(x);
	int ly = getfl(y);
	fr[rx] = ly;
	r[rx] = ly;
	fl[ly] = rx;
	l[ly] = rx; 
}
int main()
{
	int n;
	scanf("%d",&n);
	init(n);
	int x,y;
	for(int i=1;i<n;i++) {
		scanf("%d%d",&x,&y);
		Merge(x,y);
	}
	int st;
	for(st=1;st<=n;st++)
		if(l[st] == st)	//左边数第一个 
			break;
	
	while(r[st] != st) {
		printf("%d ",st);
		st = r[st];
	} 
	printf("%d\n",st);

	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_42765557/article/details/90144651
今日推荐