F. Asya And Kittens
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 之间的隔板打开,要求每次只打开一个板。
由于关系是按照顺序执行的,所以现在询问原来的n个数可能的位置。
三、思路
比赛的时候想到了类似拓扑的思路,试图沿着入度为1的点一直到达树的叶子,但是发现块与块之间的顺序无法处理,一直WA。
后来看到大佬的思路,用并查集维护块,然后用pre[ ]和nex[ ] 数组维护这个块后面的和前面的,然后再从入度为1的点向下就行了。
四、代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
int n;
int f[150005],nex[150005],pre[150005],ind[150005];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
vector<int>e[150005];
void DFS(int nx,int p)
{
printf("%d ",nx);
for(int i=0;i<e[nx].size();i++)
{
int to =e[nx][i];
if(to!=p)DFS(to,nx);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)pre[i]=f[i]=i,nex[i]=i,ind[i]=0;
for(int i=1;i<=n-1;i++)
{
int x,y;
scanf("%d %d",&x,&y);
int fx=find(x),fy=find(y);
int r=nex[fx],l=pre[fy];
e[r].push_back(l);e[l].push_back(r);
ind[l]++;ind[r]++;
nex[fx]=nex[fy];pre[fy]=pre[fx];
f[fy]=fx;
}
for(int i=1;i<=n;i++)
{
if(ind[i]==1)
{
DFS(i,-1);break;
}
}
return 0;
}