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

题干:

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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439565.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

详解道路标记数据集 CeyMo: See More on Roads -- A Novel Benchmark Dataset for Road Marking Detection

本文介绍一个新的道路标记检测数据集&#xff0c;论文收录于 WACV2022。Ceymo数据集总共包含2887张图片&#xff0c;标注了11类共4706个道路标记实例&#xff0c;图片分辨率为 192010801920\times108019201080。其中&#xff0c;对于每一个道路标记实例&#xff0c;作者采用了三…

动手学无人驾驶(7):车道线检测

最近在研究视觉语义地图&#xff0c;需要进行车道线检测&#xff0c;发现这篇车道线检测论文效果蛮好的 &#xff08;Ultra Fast Structure-aware Deep Lane Detection&#xff09;。论文作者在知乎上已经介绍过了&#xff1a;https://zhuanlan.zhihu.com/p/157530787&#xff…

Coursera自动驾驶课程第16讲:LIDAR Sensing

在第15讲《Coursera自动驾驶课程第15讲&#xff1a;GNSS and INS Sensing for Pose Estimation》 我们学习了自动驾驶定位中常用的两种传感器&#xff1a;IMU&#xff08;惯性测量单元&#xff09; 和GNSS&#xff08;全球导航卫星系统&#xff09;。 本讲我们将学习自动驾驶汽…

DB、ETL、DW、OLAP、DM、BI关系结构图

在此大概用口水话简单叙述一下他们几个概念&#xff1a; &#xff08;1&#xff09;DB/Database/数据库——这里一般指的就是OLTP数据库&#xff0c;在线事物数据库&#xff0c;用来支持生产的&#xff0c;比如超市的买卖系统。DB保留的是数据信息的最新状态&#xff0c;只有一…

Tarjan 算法 常用模板

可以求每个点属于第几个强连通分量&#xff1a;https://blog.csdn.net/dellaserss/article/details/8267192 int Tarjan(int u){int v;dfn[u]low[u]Index;stack[Top]u;Instack[u]1;for(int i0;i<G[u].size();i){vG[u][i];if(!dfn[v]){Tarjan(v);low[u]min(low[u],low[v]);}…

【HDU - 5012】Dice(模拟,bfs)

题干&#xff1a; There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a 1.a 2,a 3,a 4,a 5,a 6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. S…

重读经典:《Generative Adversarial Nets》

GAN论文逐段精读【论文精读】这是李沐博士论文精读的第五篇论文&#xff0c;这次精读的论文是 GAN。目前谷歌学术显示其被引用数已经达到了37000。GAN 应该是机器学习过去五年上头条次数最多的工作&#xff0c;例如抖音里面生成人物卡通头像&#xff0c;人脸互换以及自动驾驶中…

一步步编写操作系统 62 函数调用约定

由于我们要将c语言和汇编语言结合编程啦&#xff0c;所以一定会存在汇编代码和c代码相互调用的问题&#xff0c;有些事情还是要提前交待给大家的&#xff0c;本节就是要给大家说下函数调用规约中的那些事儿。 函数调用约定是什么&#xff1f; 调用约定&#xff0c;calling co…

重读经典:《An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale》

ViT论文逐段精读【论文精读】这次李沐博士邀请了亚马逊计算机视觉专家朱毅博士来精读 Vision Transformer&#xff08;ViT&#xff09;&#xff0c;强烈推荐大家去看本次的论文精读视频。朱毅博士讲解的很详细&#xff0c;几乎是逐词逐句地讲解&#xff0c;在讲解时把 ViT 相关…

给不会调用C++STL库中二分函数lower_bound,upper_bound,binary_search同学的一些话!

lower_bound算法返回第一个大于等于给定值所在的位置。设置两个指针start和last&#xff0c;其中start指向数组的起始位置&#xff0c;last指向数组末尾位置之后的位置。当start和last指向相同位置时循环结束。mid指向[start,last)区间的中间位置&#xff0c;当中间位置元素值大…

详解IMU标定经典论文:A Robust and Easy to Implement Method for IMU Calibration without External Equipments

本文介绍一篇 关于IMU 标定的经典论文&#xff0c;论文收录于 ICRA14&#xff0c;在论文中作者介绍了如何不适用外部设备标定 IMU 加速度和角速度偏差、尺度系数、轴偏移参数。 论文链接&#xff1a;https://readpaper.com/paper/2021503353、https://readpaper.com/paper/221…

重读经典:《Momentum Contrast for Unsupervised Visual Representation Learning》

MoCo 论文逐段精读【论文精读】这次论文精读李沐博士继续邀请了亚马逊计算机视觉专家朱毅博士来精读 Momentum Contrast&#xff08;MoCo)&#xff0c;强烈推荐大家去看本次的论文精读视频。朱毅博士和上次一样讲解地非常详细&#xff0c;几乎是逐词逐句地讲解&#xff0c;在讲…

【HRBUST - 1623】Relation(思维模拟,拆解字符串)

题干&#xff1a; 一天&#xff0c;ikki在看书的时候发现书上有个类似于家谱状的插图&#xff0c;突然ikki想到了一个有趣的现象&#xff1a;有时候用某个人一连串 的关系描述另一个人的时候&#xff0c;最后可能还是他本身。例如&#xff1a;小明的爸爸的爸爸和小明的爷爷是同…

一步步编写操作系统 67 系统调用的实现1-2 68

接上文&#xff1a; 系统调用的子功能要用eax寄存器来指定&#xff0c;所以咱们要看看有哪些系统调用啦&#xff0c;在linux系统中&#xff0c;系统调用是定义在/usr/include/asm/unistd.h文件中&#xff0c;该文件只是个统一的入口&#xff0c;指向了32位和64位两种版本。在a…

【HDU - 6662】Acesrc and Travel(树形dp,博弈dp)

题干&#xff1a; Acesrc is a famous tourist at Nanjing University second to none. During this summer holiday, he, along with Zhang and Liu, is going to travel to Hong Kong. There are nnspots in Hong Kong, and n−1n−1 bidirectional sightseeing bus routes …

一步步编写操作系统 69 汇编语言和c语言共同协作 70

由于有了上一节的铺垫&#xff0c;本节的内容相对较少&#xff0c;这里给大家准备了两个小文件来实例演示汇编语言和c语言相互调用。 会两种不同语言的人&#xff0c;只是掌握了同一件事物的两种表达方式。人在学习一种新语言时&#xff0c;潜意识里是建立了语言符号与事物形象…

一步步编写操作系统 71 直接操作显卡,编写自己的打印函数71-74

一直以来&#xff0c;我们在往屏幕上输出文本时&#xff0c;要么利用bios中断&#xff0c;要么利用系统调用&#xff0c;这些都是依赖别人的方法。咱们还用过一个稍微有点独立的方法&#xff0c;就是直接写显存&#xff0c;但这貌似又没什么含量。如今我们要写一个打印函数了&a…

【CodeForces - 208C 】Police Station(单源最短路条数,起点终点建图,枚举顶点)

题干&#xff1a; The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possi…

【Chrome浏览器】常用快捷键整理

标签页和窗口快捷键 1. Ctrl n 打开新窗口 2. Ctrl t 打开新的标签页&#xff0c;并跳转到该标签页 3. Ctrl Shift t 重新打开最后关闭的标签页&#xff0c;并跳转到该标签页 4. Ctrl Tab 跳转到下一个打开的标签页 5. Ctrl Shift Tab 跳转到上一个打开的标签页 6.…