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. linux系统无法上外网,路由器可以上网,可以ping通路由器,ping不通外网IP

    临时生效方法(添加路由网关),执行: #route add default gw 192.168.92.1   #根据实际网关IP填写 如果不行,使用下面方法: 一:使用 route 命令添加使用ro ...

  2. [OS][FS]查看ext3文件系统分区的superblock

    本文将介绍怎样读取一个分区的superblock: 1. 首先我们查看一下在磁盘上有哪些分区,通过fdisk -l 这里有三个分区,我们下面查看/dev/sda3(这是一个device file) 2 ...

  3. MVC视图中下拉框的使用

    一.一般变量或对象的绑定 首先要在controller 中将选项设置成 selecList对象,并赋值给viewBag动态对象. public ActionResult Index(string mo ...

  4. JavaScript 算法与数据结构(转载)

    JavaScript 算法与数据结构 https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-CN.md

  5. RedHat/CentOS安装五笔输入法(转载)

    转自:http://www.zhukun.net/archives/5939 使用centos 仓库里的 ibus,我的 CenOS6.3 自带了 ibus 包.打开 System – prefere ...

  6. TCP协议中的三次握手和四次挥手(图解)【转载】

    建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...

  7. bzoj 2839: 集合计数【容斥原理+组合数学】

    首先,考虑容斥,我们所要的答案是并集至少有\( k \)个数的方案数减去并集至少有\( k+1 \)个数的方案数加上并集至少有\( k \)个数的方案数-- 在n个数中选i个的方案数是\( C_{n} ...

  8. Quartz.Net实现的定时执行任务调度

    在之前的文章<推荐一个简单.轻量.功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler>和<简单.轻量.功能非常强大的C#/ASP.NET定时调度 ...

  9. Unix\Linux | 总结笔记 | 邮件发送

    实验:在本地实现不同用户收发邮件 #root发送邮件 #stu 收邮件 #stu 查看邮件 并回复邮件 #root 查看stu回复的邮件

  10. _bzoj3223 Tyvj 1729 文艺平衡树【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 裸的,打个标记. #include <cstdio> #include & ...