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.

题意就是给你两个数合并的顺序让你构造序列。

一开始没想起来,以为是单链表,突然想到要有前驱,就想到双向链表了,但是一直卡D,没时间了,这是一个过的比D多的F题。。。

思路就是双向链表+并查集,具体的代码写了注释。

两种代码,一个是模拟的双向链表+并查集,一个是用STL的list+list的splice()函数。

看的某大佬群里大佬的代码。

代码一:

 //F-双向链表(模拟)+并查集
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=*1e5+; int l[maxn],r[maxn],nex[maxn],root[maxn]; int findr(int x)//递归并查集
{
if(root[x]==x) return x;
return root[x]=findr(root[x]);
} int main()
{
int n,u,v;
cin>>n;
for(int i=;i<=n;i++)
root[i]=l[i]=r[i]=i;//初始化
while(n>){
n--;
cin>>u>>v;
u=findr(u);
v=findr(v);
root[v]=u;//并查集合并
nex[r[u]]=l[v];//当前u的最右端的节点与v的最左端的节点相连
r[u]=r[v];//将u的最右端节点更新为v的最右端节点
}
u=l[findr()];//找到头节点
while(u){
cout<<u<<" ";
u=nex[u];//按照连接顺序输出
}
}

代码二:

 //F-STL list(双向链表)+list拼接合并 splice函数(实现并查集的操作)
/*
list的splice函数主要是用来合并两个list
splice函数是list中特有的拼接方式,splice实现了不需要拷贝的list合并,
即可以在常数时间内从list的一个区域拼接到另一个list的一个区域。
也就是说splice是一个常数时间的函数(但是也会产生其他形如list的size()问题,导致size()处理时间为O(n)不为常数)
l1.splice(l1.begin(),it);//(要插入的位置迭代器,要插入的元素的迭代器)
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=*1e5+; int fa[maxn]; int find(int x)//递归并查集
{
return x==fa[x]?x:fa[x]=find(fa[x]);
} list<int> h[maxn]; int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++){//初始化,一开始各个list里面只有自己,父节点就是自己
h[i].push_back(i);
fa[i]=i;
}
for(int i=;i<n;i++){
int x,y;
cin>>x>>y;
x=find(x);
y=find(y);
fa[y]=x;//并查集节点合并
h[x].splice(h[x].end(),h[y]);//在h[x]的尾部插入h[y]
}
for(auto it:h[find()]){
cout<<it<<" ";
}
cout<<endl;
}

想晚了,对不起,比赛的时候没出来这道题。。。

Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))的更多相关文章

  1. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  2. codeforces #541 F Asya And Kittens(并查集+输出路径)

    F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...

  3. F. Asya And Kittens并查集

    F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #541 (Div. 2) (A~F)

    目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...

  5. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  6. Codeforces Round #541 (Div. 2)

    Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...

  7. Codeforces Round #541 F. Asya And Kittens

    题面: 传送门 题目描述: Asya把N只(从1-N编号)放到笼子里面,笼子是由一行N个隔间组成.两个相邻的隔间有一个隔板. Asya每天观察到有一对想一起玩,然后就会把相邻的隔间中的隔板取出来,使两 ...

  8. Codeforces Round #541 (Div. 2)题解

    不知道该更些什么 随便写点东西吧 https://codeforces.com/contest/1131 ABC 太热了不写了 D 把相等的用并查集缩在一起 如果$ x<y$则从$ x$往$y$ ...

  9. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

随机推荐

  1. idea 创建多模块时模块类无法引入

    我的原因是类的位置方的不对,由于刚搭建的项目,本来只想做个测试,就直接在java下创建类,然而这居然是个深坑,模块引入了也无法引入这个模块的类. 解决方法:创建com.***.***包后的类可以正常引 ...

  2. Itext2.0.8 和freemarker导出pdf

    这个是跟上一篇写的freemarker导出word是一块的.但是关联性不是很大.由于本人技术有限本篇导出也是根据网上大家的做出的demo混合而成.有不足的地方请大家指出.好改正,使以后看到的freem ...

  3. UVA 1645 Count

    https://vjudge.net/problem/UVA-1645 题意:有多少个n个节点的有根树,每个深度中所有节点的子节点数相同 dp[i] 节点数为i时的答案 除去根节点还有i-1个点,如果 ...

  4. [POI2009]WIE-Hexer

    https://www.luogu.org/problem/show?pid=3489 题目描述 Byteasar has become a hexer - a conqueror of monste ...

  5. 概率dp+期望dp 题目列表(一)

    表示对概率和期望还不是很清楚定义. 目前暂时只知道概率正推,期望逆推,然后概率*某个数值=期望. 为什么期望是逆推的,例如你求到某一个点的概率我们可以求得,然后我们只要运用dp从1~n每次都加下去就好 ...

  6. 51nod 1170 1770 数数字(数学技巧)

    解题思路:看到题后,直接想到分成两种情况: ①:a*b >9 这里又分成两种 1. n==1 a*b 直接是一个两位数 求得十位和个位(这里十位和个位不可能相等) 然后如果等于d 则结果=1 2 ...

  7. 解决SpringSecurity限制iframe引用页面的问题

    使用Spring Security的过程中,需要使用iframe来引入其他域的页面,页面会报X-Frame-Options的错误,试了好几种方法一直未能很好的解决这个问题. 这里涉及到Spring S ...

  8. UIImageView与UIScrollView的关系图

        UIImageView与UIScrollView的关系图           https://www.evernote.com/shard/s227/sh/0af9f23c-08e6-4be6 ...

  9. R、Python、Scala和Java,到底该使用哪一种大数据编程语言?

    有一个大数据项目,你知道问题领域(problem domain),也知道使用什么基础设施,甚至可能已决定使用哪种框架来处理所有这些数据,但是有一个决定迟迟未能做出:我该选择哪种语言?(或者可能更有针对 ...

  10. Centos 6.5下安装vsftpd服务器

    1.查看是否安装vsftp  [root@localhost ~]#rpm -qa|grep vsftpd 如果出现 vsftpd-2.2.2-13.el6_6.1.x86_64  则说明已经安装了v ...