题干:
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 ;
}