Codeforces 1131 F.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

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.

 

題意:

給一個長度爲n的格子,每個格子之間有擋板,現給出n-1對數 (x y),每給出一對可撤掉一塊擋板,合併x,y所在的兩個集合,問原序列是什麼

 

解析:

並查集合並集合+DFS遞歸輸出,我們以樣例爲例子,設G[ ][ ]爲一個圖,初始化pre[ i ]==i,表示每個節點祖先都是自身

G[1]={ 4 } 表示1和4相連合並,pre[4]=1

G[2]={ 5 } 表示2和5相連合並,pre[5]=2

G[3]={ 1 } 3和1合併,pre[1]=3

G[4]={ 2 } 4和2合併,爲什麼不是5呢,因爲pre[5]=2,也就是說5有一個前驅是2,所以要率先和前驅合併,pre[ ]的作用就在於此

 

代碼如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int>vec[maxn];
int n;
bool vis[maxn];
int pre[maxn];
int find(int x) {//find函數找前驅 
	if (x == pre[x])
		return x;
	return pre[x] = find(pre[x]);
}
void init()
{
	for (int i = 0; i <= n; i++) 
		pre[i] = i;
}
void dfs(int x) {//DFS輸出路徑 
	cout << x << ' ';
	for (int i = 0; i<vec[x].size(); i++) {
		dfs(vec[x][i]);
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	int x, y;
	init();
	for (int i = 1; i<n; i++) {
		cin >> x >> y;
		int fx = find(x);//找祖先,也就是前驅 
		int fy = find(y);
		pre[fy] = fx;	
		vec[fx].push_back(fy);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {//第一個節點祖先肯定是自身 
		if (pre[i] == i) {
			ans = i;
			break;
		}
	}
	dfs(ans);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章