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. Agent Job代理 执行SSIS Package

    摘要: 在使用Agent Job时, 运行SSIS包的Run as账号,必须有SSIS中connection manager的连接权限. 如果没有连接权限,可以用创建proxy账号,并确保proxy账 ...

  2. eclipse代码提示设置过常用字符还是不起作用的解决方法

    问题:重装eclipse之后发现没有了代码提示,一般情况下在设置中添加自动提示的字符之后就可以了,设置如下 如上图,初始的时候是只有一个点号,并没有字符,输入26个字母的大小写后点击Apply and ...

  3. Java中char,short,int,long占几个字节和多少位

    1.字节:byte:用来计量存储容量的一种计量单位:位:bit 2.一个字节等于8位  1byte = 8bit char占用的是2个字节 16位,所以一个char类型的可以存储一个汉字. 整型: b ...

  4. KeyboardUtil【软键盘弹出后输入框上移一定的高度】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 演示获取软键盘高度并保存,然后根据输入框的原有位置是否被软键盘挡住了,如果被挡住了则将整体页面上移一定的高度,当软键盘隐藏的时候再下 ...

  5. Devexpress 常用的功能

    一 .GridControl的删除操作 private void rILinkEditInfoDel_Click(object sender, EventArgs e) { if (XtraMessa ...

  6. Virtual Box虚拟机Ubuntu18.X系统安装及Mysql基本开发配置

    Linux简介 什么是 Linux? Linux:世界上不仅只有一个 Windows 操作系统,还有 Linux.mac.Unix 等操作系统.桌面操作系统下 Windows 是霸主,而 Linux ...

  7. k8s健康检查(七)--技术流ken

    默认的健康检查 强大的自愈能力是 Kubernetes 这类容器编排引擎的一个重要特性.自愈的默认实现方式是自动重启发生故障的容器.除此之外,用户还可以利用 Liveness 和 Readiness ...

  8. PyTorch入门(一)向量

    什么是PyTorch?   PyTorch是Facebook人工智能团队开发的一个机器学习和深度学习工具,用于处理大规模图像分析,包括物体检测,分割与分类.但是它的功能不仅限于此.它与其它深度学习框架 ...

  9. SqlSugar ORM 入门篇2 【查询】 让我们实现零SQL

    SqlSugar在查询的功能是非常强大的,多表查询.分页查询 . 一对一查询.二级缓存.一对多查.WhenCase等复杂函数.Mapper功能.和拉姆达自定义扩展等,用好了是可以做到真正零SQL的一款 ...

  10. 了解AJAX

    1.如何打开终端的快捷键 Window+R==>CMD==>ipconfig 动态页面:跟后台发生数据交互的页面. 前后台数据交互依赖的一项技术叫 ajax. 1. js的异步操作 (1) ...