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.

用并查集模拟,同时维护两边的位置即可

#include<bits/stdc++.h>
using namespace std;
const int N=;
int n,d[N],pre[N],nxt[N],f[N];
vector<int>g[N];
void adde(int u,int v){
g[u].push_back(v);
g[v].push_back(u);
d[u]++;
d[v]++;
}
void dfs(int u,int fa){
printf("%d ",u);
for(int i=;i<(int)g[u].size();++i){
if(g[u][i]!=fa)dfs(g[u][i],u);
}
}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int main(){
scanf("%d",&n);
for(int i=;i<=n;++i)pre[i]=nxt[i]=f[i]=i;
for(int i=;i<n;++i){
int x,y;
scanf("%d%d",&x,&y);
x=find(x),y=find(y);
adde(nxt[x],pre[y]);
nxt[x]=nxt[y];
f[y]=x;
}
int rt=;
for(int i=;i<=n;++i)if(d[i]==){rt=i;break;}
dfs(rt,);
return ;
}

F. Asya And Kittens并查集的更多相关文章

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

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

  2. F. Asya And Kittens 并查集维护链表

    reference :https://www.cnblogs.com/ZERO-/p/10426473.html

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

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

  4. Codeforces Round #541 F. Asya And Kittens

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

  5. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集

    题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...

  6. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  7. [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]

    题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...

  8. codeforces 659F F. Polycarp and Hay(并查集+bfs)

    题目链接: F. Polycarp and Hay time limit per test 4 seconds memory limit per test 512 megabytes input st ...

  9. [原]武大预选赛F题-(裸并查集+下标离散化+floyd最短路)

    Problem 1542 - F - Countries Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 266 Accepted: 36 ...

随机推荐

  1. OSD锁定怎么解锁?

    方法是这样的: 先按中间的建关掉显示器电源,关了显示器后按住左键,在按中间的建开机,这时屏幕闪一下就解锁了.在按中间的建打开显示器就行了. 加锁的方法和解锁一样

  2. 思维导图js

    http://baike.baidu.com/view/1469162.htm?fromenter=Mind+Maps

  3. Surface pro4 触摸板手势快捷键

    [一个手指]:这个大家都知道就不必在介绍了,跟之前win7一样,滑动就是鼠标,轻点(或者按左下方)就相当于点鼠标左键.(两个按键按起来真心手感不咋的,所以基本不用)[两个手指]:1.两个手指向上或下滑 ...

  4. combox组合框设置高度

    组合框设置高度 转载 2013年10月24日 22:54:03 1033 MFC进行界面编程时,组合框CComboBox控件在可视化设计组件的时候是无法进行高度编辑的,但是我们在实际的项目中经常需要定 ...

  5. Vue解决安卓4.4不兼容的问题

    1.npm安装 npm install babel-polyfillnpm install es6-promise package.json中会出现 "babel-polyfill" ...

  6. tomcat 配置图片虚拟路径不起作用解决办法

    最近在做一个小项目,用到了图片上传服务器,以前尝试过实现这个功能Demo,虽然基本功能没有问题,但是很不完善,当时也有在博客记录, 地址如下: http://www.jb51.net/article/ ...

  7. 【WIP】gollum

    创建: 2018/03/18 [任务表]TODO 这个博客从来不点发布到首页, 完全100%自用, 全部详细完整的干货.千辛万苦找到这里看到一片空白, 是不是很愤怒? 那就对啦233333

  8. E20170520-ts

    regexp  n. 正则表达式 metacharacter メタ文字 元字符 quote   vt. 引述,引用; escape  v. 逃脱; 逃离;   n. 逃走; 逃跑工具或方法;  reg ...

  9. 【性能测试】服务器资源监测工具sar安装

    [root@yyy ~]# sar Cannot open /var/log/sa/sa19: No such file or directory 在Linux系统中,运行sar命令,发现无法执行: ...

  10. HDU1429:胜利大逃亡(续)

    传送门 题意 给出一个迷宫,门需要钥匙来打开,t秒内能否从起点到达终点 分析 这题我用以前一道题的代码改了改就过了,具体思想:设置vis[status][x][y],status记录到达该点拥有的钥匙 ...