F. Asya And Kittens并查集
2 seconds
256 megabytes
standard input
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.
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.
5
1 4
2 5
3 1
4 5
3 1 4 2 5
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并查集的更多相关文章
- codeforces #541 F Asya And Kittens(并查集+输出路径)
F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...
- F. Asya And Kittens 并查集维护链表
reference :https://www.cnblogs.com/ZERO-/p/10426473.html
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces Round #541 F. Asya And Kittens
题面: 传送门 题目描述: Asya把N只(从1-N编号)放到笼子里面,笼子是由一行N个隔间组成.两个相邻的隔间有一个隔板. Asya每天观察到有一对想一起玩,然后就会把相邻的隔间中的隔板取出来,使两 ...
- 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 ...
- 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 ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- 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 ...
- [原]武大预选赛F题-(裸并查集+下标离散化+floyd最短路)
Problem 1542 - F - Countries Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 266 Accepted: 36 ...
随机推荐
- JAVASCRIPT 和 AJax 实现局部验证
JSP页面 <td width="10%" class="main_matter_td">真实姓名</td> <td width= ...
- GC回收算法--当女友跟你提分手!
Java语言引入了垃圾回收机制,让C++语言中令人头疼的内存管理问题迎刃而解,使得我们Java狗每天开开心心地创建对象而不用管对象死活,这些都是Java的垃圾回收机制带来的好处.但是Java的垃圾回收 ...
- KMP算法在字符串中的应用
KMP算法是处理字符串匹配的一种高效算法 它首先用O(m)的时间对模板进行预处理,然后用O(n)的时间完成匹配.从渐进的意义上说,这样时间复杂度已经是最好的了,需要O(m+n)时间.对KMP的学习可以 ...
- 《StarGAN: Unified Generative Adversarial Networks for Multi-Domain Image-to-Image Translation》论文笔记
---恢复内容开始--- Motivation 使用单组的生成器G和判别训练图片在多个不同的图片域中进行转换 效果确实很逆天,难怪连Good Fellow都亲手给本文点赞 Introduction 论 ...
- OSI模型与TCP/IP模型基础
一.OSI七层模型 OSI(Open System Interconnection),OSI是一个开放性的通行系统互连参考模型,是一个协议规范.OSI七层模型是一种框架性的设计方法 ,建立七层模型的主 ...
- CodeForces 721A One-dimensional Japanese Crossword (水题)
题意:给定一行字符串,让你输出字符‘B'连续出现的次数. 析:直接扫一下就OK了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...
- bzoj 1898: [Zjoi2005]Swamp 沼泽鳄鱼【dp+矩阵快速幂】
注意到周期234的lcm只有12,也就是以12为周期,可以走的状态是一样的 所以先预处理出这12个状态的转移矩阵,乘起来,然后矩阵快速幂优化转移k/12次,然后剩下的次数暴力转移即可 #include ...
- golang——关于for循环的学习
1.for循环的用法 (1)常规用法 func main() { slice := []int{1, 2, 3, 4, 5, 6} //方式1 for i := 0; i < len(slice ...
- cxf CXF搭建webService服务器
http://observer.blog.51cto.com/4267416/1231205 手动发布: public class ServerMain { public static void ma ...
- SAMP论文学习
SAMP:稀疏度自适应匹配追踪 实际应用中信号通常是可压缩的而不一定为稀疏的,而且稀疏信号的稀疏度我们通常也会不了解的.论文中提到过高或者过低估计了信号的稀疏度,都会对信号的重构造成影响.如果过低估计 ...