Problem   Codeforces #541 (Div2) - F. Asya And Kittens

Time Limit: 2000 mSec

Problem Description

Input

The first line contains a single integer nn (2≤n≤150000) — the number of kittens.

Each of the following n−1lines contains integers xi and yi (1≤xi,yi≤n, xi≠yi) — indices of kittens, which got together due to the border removal on the corresponding day.

It's guaranteed, that the kittens xi and yi were in the different cells before this day.

Output

For every cell from 1 to n print a single integer — the index of the kitten from 1 to n, 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.

Sample Input

5
1 4
2 5
3 1
4 5

Sample Output

3 1 4 2 5

题解:并查集+链表,始终把y所在的集合加到x所在集合的右边,让每个集合的根始终保持在最左边,维护一下每个集合最左边元素,链表连一连即可,这种题拼的就是手速。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); int n;
int fa[maxn], ri[maxn];
int ans[maxn];
int edge[maxn]; int findn(int x)
{
return x == fa[x] ? x : fa[x] = findn(fa[x]);
} void init()
{
for (int i = ; i < maxn; i++)
{
fa[i] = edge[i] = ri[i] = i;
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
init();
cin >> n;
int x, y;
for (int i = ; i < n - ; i++)
{
cin >> x >> y;
int fx = findn(x), fy = findn(y);
fa[fy] = fx;
ri[edge[fx]] = fy;
edge[fx] = edge[fy];
}
int root = -;
for (int i = ; i <= n; i++)
{
if (findn(i) == i)
{
root = i;
}
}
ans[] = root;
int cnt = ;
for (int i = ri[root]; cnt++; i = ri[i])
{
ans[cnt] = i;
if (cnt == n)
break;
}
for(int i = ; i <= n; i++)
{
cout << ans[i];
if(i != n)
{
cout << " ";
}
else
{
cout << endl;
} }
return ;
}

Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)的更多相关文章

  1. codeforces #541 F Asya And Kittens(并查集+输出路径)

    F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...

  2. F. Asya And Kittens并查集

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

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

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

  4. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  7. Codeforces Round #541 F. Asya And Kittens

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

  8. Codeforces #451 Div2 F

    #451 Div2 F 题意 给出一个由数字组成的字符串,要求添加一个加号和等号,满足数字无前导 0 且等式成立. 分析 对于这种只有数字的字符串,可以快速计算某一区间的字符串变成数字后并取模的值,首 ...

  9. Codeforces #452 Div2 F

    #452 Div2 F 题意 给出一个字符串, m 次操作,每次删除区间 \([l,r]\) 之间的字符 \(c\) ,输出最后得到的字符串. 分析 通过树状数组和二分,我们可以把给定的区间对应到在起 ...

随机推荐

  1. @SuppressWarnings("rawtypes") 是什么含义

    简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...

  2. mac电脑 上强大的RAW图像处理工具 ——RAW Power

    苹果电脑曾经有一款名为Aperture的照片处理应用,最终因为苹果软件策略的更好与升级,这款应用已经被苹果砍掉.但Aperture的开发者们并未放弃这款应用,在Mac OS上推出了一款名为RAW Po ...

  3. ionic4 混合移动开发 (前世今生)

    ionic 从2016年初识,经历了 ionic2 ionic3.至今 ionic4,终于在2018年7月份发布了测试版. ionic Framework 可以说得上是最接近原生app的ui组件,漂亮 ...

  4. golang的cms

    golang的cms 说说cms cms(内容管理系统)是建站利器.它的本质是为了快速建站.cms本质是一个后台服务站,使用这个后台,能很快搭建一个前台web站.在PHP的世界里面,CMS框架简直不要 ...

  5. springboot情操陶冶-web配置(九)

    承接前文springboot情操陶冶-web配置(八),本文在前文的基础上深入了解下WebSecurity类的运作逻辑 WebSecurityConfigurerAdapter 在剖析WebSecur ...

  6. 痞子衡嵌入式:ARM Cortex-M内核MCU开发那些事 - 索引

    大家好,我是痞子衡,是正经搞技术的痞子.本系列痞子衡给大家介绍的是ARM Cortex-M内核微控制器相关知识. ARM公司从2004年开始推出Cortex-M系列内核,迄今Cortex-M家族已经包 ...

  7. 【转载】java final 关键字的几种用法

    原文链接点这里,感谢博主分享 在java的关键字中,static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构 ...

  8. Linux通配符应用详解

    一.强大的“*” “*”在通配符中是最常用的一种,代表0个.一个或多个字符.在使用中有三种情况,表示三种不同的含义. 1.单独的“*” 这里指的是只有“*”出现的情况,默认为单独的一个,当然连续敲两个 ...

  9. 分享基于 websocket 网页端聊天室

    博客地址:https://ainyi.com/67 有一个月没有写博客了,也是因为年前需求多.回家过春节的原因,现在返回北京的第二天,想想,应该也要分享技术专题的博客了!! 主题 基于 websock ...

  10. 限制TextBox只允许输入数字和字母

    设置TextBox控件属性 ImeMode=Disable ShortcutsEnabled=False VB.NET Private Sub TextBox1_KeyDown(sender As O ...