F. 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 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
5
1 4
2 5 
3 1
4 5
output
3 1 4 2 5
 
题意:模拟合并两个连通块
思路:用链表的思想去合并
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int f[],n;
int path[],last[]; //path记录路径 last记录最后一个数
int find(int x){
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
void join(int x,int y){
int xx=find(x);
int yy=find(y);
if(xx!=yy)
f[yy]=xx;
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=;i<=n;i++) f[i]=i,last[i]=i;
n--;
while(n--){
int x,y; cin>>x>>y;
int xx=find(x); int yy=find(y);
if(xx==yy) continue;
path[last[xx]]=yy;
last[xx]=last[yy];
join(x,y);
}
for(int i=find();i;i=path[i])
cout<<i<<" ";
return ;
}
 

codeforces #541 F Asya And Kittens(并查集+输出路径)的更多相关文章

  1. F. Asya And Kittens并查集

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

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

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

  3. 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 ...

  4. Codeforces Round #541 F. Asya And Kittens

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

  5. codeforces #541 D. Gourmet choice(拓扑+并查集)

    Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...

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

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

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

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

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

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

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

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

随机推荐

  1. babel(一)

    一.babel npm babel src/index.js -d lib 二.@babel/core   @babel/cli @babel/core  转换语法核心 @babel/cli   执行 ...

  2. C#设计模式之6:抽象工厂模式

    前面分析了简单工厂模式和工厂方法模式,接着来看一下抽象工厂模式,他与工厂方法模式有一些相似的地方,也有不同的地方. 先来看一个不用工厂方法模式实现的订购披萨的代码: 对象依赖的问题:当你直接实例化一个 ...

  3. CMake--变量

    1.一般变量 1)CMake变量引用的方式 使用${}进行变量的引用.例如: ${PROJECT_NAME} #返回项目名称 在 IF 等语句中,是直接使用变量名而不通过${}取值. 2)cmake自 ...

  4. 如何让pl/sql developer记住密码,实现快速登录

    前两天,有同事使用plsql的时候,切换数据库的时候需要不断的重复输入密码,这样太麻烦了. 下面,我这里说下如何的实现plsql不需要输入密码就能快速登录的方法: 1.一开始登录,首先像往常那样输入密 ...

  5. redis 的简单命令

    以下实例讲解了如何启动 redis 客户端: 启动 redis 客户端,打开终端并输入命令 redis-cli.该命令会连接本地的 redis 服务. $redis-cli redis > re ...

  6. springmvc中model可以封装的数据类型

    查看源码可以知道,model中可以存放的数据类型 Model addAttribute(String var1, @Nullable Object var2); Model addAttribute( ...

  7. Nginx安装- CentOS7

    1.确认是否具备安装环境 g++  -v 如果不打印则不具备. 解决办法:联网执行如下命令 yum install gcc yum install gcc-c++ 2.需要材料 pcre-8.37.t ...

  8. python 网络编程 IO多路复用之epoll

    python网络编程——IO多路复用之epoll 1.内核EPOLL模型讲解     此部分参考http://blog.csdn.net/mango_song/article/details/4264 ...

  9. todo项目总结

    vue+webpack项目工程配置 1.vue-loader+webpack项目配置 2.webpack配置项目加载各种静态资源 3.webpack-dev-server的配置和使用 安装: pack ...

  10. css last