日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 1131F 】Asya And Kittens(并查集,思维)

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 1131F 】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?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?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?11to?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.

題目大意:

有n只貓,每個都被擋板單獨分隔,給出n-1個操作xy,表示將x,y之間的隔板打開,每次只打開一個板,且保證輸入合法(即不會出現拿起一個已經拿開的擋板),所以最終所有貓都挨著了。操作是按照順序執行的。現在詢問原來的n只貓可能的位置。

第一行輸入n,接下來n-1行,每行x和y代表將x,y之間的隔板打開。

解題報告:

因為具有傳遞性,考慮并查集。不難發現,每次拿開一個擋板,就會確定兩只貓的相鄰位置關系,所以需要維護的信息就是集合的最左側的貓的編號和最右側的貓的編號,這樣合并兩個集合的時候就可以直接讓左邊的集合的最右側的貓和右邊的集合的最左側的貓合并即可。同時用一個數組ne維護貓之間的相鄰位置(跟鏈表類似),然后讓父節點總是最左側節點,新開一個數組記錄當前集合的最右側節點,然后直接更新就好。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int f[MAX],R[MAX],ne[MAX]; int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } int main() {int n;cin>>n;for(int i = 1; i<=n; i++) f[i]=i,R[i]=i;for(int u,v,i = 1; i<=n-1; i++) {scanf("%d%d",&u,&v);u=getf(u),v=getf(v);ne[R[u]]=v;R[u]=R[v];f[v]=u;}for(int i = getf(1); i;i = ne[i]) printf("%d ",i);return 0 ; }

?

總結

以上是生活随笔為你收集整理的【CodeForces - 1131F 】Asya And Kittens(并查集,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。