codeforces #541 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.
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.
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.
#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(并查集+输出路径)的更多相关文章
- F. Asya And Kittens并查集
F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- F. Asya And Kittens 并查集维护链表
reference :https://www.cnblogs.com/ZERO-/p/10426473.html
- 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 ...
- Codeforces Round #541 F. Asya And Kittens
题面: 传送门 题目描述: Asya把N只(从1-N编号)放到笼子里面,笼子是由一行N个隔间组成.两个相邻的隔间有一个隔板. Asya每天观察到有一对想一起玩,然后就会把相邻的隔间中的隔板取出来,使两 ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- 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 ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
随机推荐
- Oracle SQL优化原则
原文:http://bbs.landingbj.com/t-0-240353-1.html 1.选用适合的 ORACLE 优化器 2.访问 Table 的方式 3.共享SQL语句 共享的语句必须满足三 ...
- JMeter压测分布式部署
监控JMeter压力机的性能
- python与C,在写程序时踩过的坑!
1. python与C有很多相似之处, 其一就是指针的大量应用, 因此在使用临时变量保存数据, 并将临时变量传递给其他变量时需要创建内存; 例如,在C中, char *temp 每次获取到不同的字 ...
- java类库
Java的应用程序接口(API)以包的形式来组织,每个包提供大量的相关类.接口和异常处理类,这些包的集合就是Java的类库. Java类库可以分为两种 包名以java开始的包是Java核心包(Java ...
- Redis 安装学习
Linux下下载安装redis https://redis.io/download tar -zvxf redisxxx cd redisxxxx make ---进行安装 vim ~.bash_p ...
- webservice服务的提供及调用完整代码示例
服务提供方: applicationContext.xml applicationContext-webService.xml 服务调用方:
- 1、通过eureka创建注册中心
第一个demo(用户需要调用电影服务) 1.创建项目 new starter project 勾选上Eureka Server 2.编写application.yml #配置端口 server: po ...
- DataSet 取值,DataSet行数,DataSet列数 从DataSet中取出特定值
1 DataSet.Table[0].Rows[ i ][ j ] 其中i 代表第 i 行数, j 代表第 j 列数 2 DataSet.Table[0].Rows[ i ].ItemArray[ j ...
- 转载:关于JESD204B转换器与FPGA匹配的设计关键点
http://www.dzsc.com/data/2014-11-27/107442.html 随着更多的模数转换器(ADC)和数模转换器(DAC)支持最新的JESD204B串行接口标准,出现了FPG ...
- codeforces546C
Soldier and Cards CodeForces - 546C Two bored soldiers are playing card war. Their card deck consist ...