题干:

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 ;
}

【CodeForces - 1131F 】Asya And Kittens(并查集,思维)相关推荐

  1. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  2. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  3. CodeForces - 1445E Team-Building(可撤销并查集)

    题目链接:点击查看 题目大意:给出一张有 n 个点和 m 条边的图,每个点都有一个种类,共有 k 个种类,现在要从 k 个种类中每次选出两种,对所有 C( k , 2 ) 种组合单独讨论,对于选出的两 ...

  4. CodeForces - 892E Envy(可撤销并查集)

    题目链接:点击查看 题目大意:给出一张由 n 个点 m 条边组成的连通图,有 q 次询问,每次询问给出一个边集,需要判断这些边是否可以同时出现在最小生成树上 题目分析:需要用到的一个性质是,对于同一个 ...

  5. CodeForces - 468B Two Sets(并查集+思维)

    题目链接:点击查看 题目大意:现在给出两个集合A和B,再给出两个数a和b,现在规定在集合A中的数x必须满足x和a-x同时在集合a中,而在集合B中的数x也同样需要满足x和b-x同时在集合B中,现在给出一 ...

  6. codeforces gym-101736 Farmer Faul 平衡树+并查集

    题目 题目链接 题意 给出nnn个整数,其中1≤n≤106" role="presentation" style="position: relative;&qu ...

  7. 【CodeForces - 731C】Socks(并查集,思维)

    题干: Arseniy is already grown-up and independent. His mother decided to leave him alone for m days an ...

  8. Codeforces 1140F 线段树 分治 并查集

    题意及思路:https://blog.csdn.net/u013534123/article/details/89010251 之前cf有一个和这个相似的题,不过那个题只有合并操作,没有删除操作,直接 ...

  9. CodeForces - 722C Destroying Array (并查集/集合的插入和删除)

    原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...

  10. CodeForces 828C String Reconstruction(并查集思想)

    题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第 ...

最新文章

  1. 商品秒杀,防并发解决思路
  2. 修改unity变量名但不丢失序列化值
  3. 深入浅出理解 Spark:环境部署与工作原理
  4. visual studio 2015提示IE10未安装
  5. python获取类的类属性_Python中如何获取类属性的列表
  6. HDU4635 Strongly connected
  7. linux创建添加用户脚本,Linux 脚本之用户创建
  8. 三级数据库知识点总结
  9. 反三角函数的积分公式
  10. 阅读-《金字塔原理》
  11. AndroidStudio配置夜神模拟器
  12. unity编辑器莫名崩溃
  13. 石灰墙上的这种污渍怎么去掉?黑色灰色的
  14. SQL获取某月第一天或最后一天
  15. 灰色预测模型及其代码
  16. 2021年金属非金属矿山(露天矿山)安全管理人员最新解析及金属非金属矿山(露天矿山)安全管理人员模拟考试
  17. c语言函数指针的理解与使用
  18. Microsoft首席信息安全官CISO研讨会白皮书:网络安全简介(01)
  19. 服务器系统查看重启记录,查询linux系统重启记录的last命令
  20. OC基础学习——基本数据类型和复杂数据类型

热门文章

  1. 浅谈程序员的职业规划
  2. 第八章xgboost/lightGBM
  3. [Android]安装 Android Studio 第一行Android代码
  4. 分类器交叉验证java_使用交叉验证的KNN分类器
  5. unionall mysql_5分钟了解MySQL5.7union all用法的黑科技
  6. t检验的p值对照表_论文数据分析实战 | 如何对汇总数据进行t检验
  7. php 显示当前年月日时分秒,php 获取当前前后年、月、星期、日、时分秒的时间...
  8. 在linux中 要删除abc目录,操作系统原理与应用(linux)A卷
  9. Project vector on to vector
  10. cs8900a网卡驱动--寄存器