Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
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(并查集+链表)的更多相关文章
- 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并查集
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 #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Codeforces Round #541 F. Asya And Kittens
题面: 传送门 题目描述: Asya把N只(从1-N编号)放到笼子里面,笼子是由一行N个隔间组成.两个相邻的隔间有一个隔板. Asya每天观察到有一对想一起玩,然后就会把相邻的隔间中的隔板取出来,使两 ...
- Codeforces #451 Div2 F
#451 Div2 F 题意 给出一个由数字组成的字符串,要求添加一个加号和等号,满足数字无前导 0 且等式成立. 分析 对于这种只有数字的字符串,可以快速计算某一区间的字符串变成数字后并取模的值,首 ...
- Codeforces #452 Div2 F
#452 Div2 F 题意 给出一个字符串, m 次操作,每次删除区间 \([l,r]\) 之间的字符 \(c\) ,输出最后得到的字符串. 分析 通过树状数组和二分,我们可以把给定的区间对应到在起 ...
随机推荐
- SQLServer之修改用户自定义数据库用户
修改用户自定义数据库用户注意事项 默认架构将是服务器为此数据库用户解析对象名时将搜索的第一个架构. 除非另外指定,否则默认架构将是此数据库用户创建的对象所属的架构. 如果用户具有默认架构,则将使用默认 ...
- July 06th. 2018, Week 27th. Friday
Life has no limitations, except the ones you make. 生命无限,除非你自我设限. From Les Brown. There would be no l ...
- kubernetes实战之运行aspnetcore webapi微服务 - kubernetes
1.预备工作 unbuntu 16.04 or above docker for linux kubernetes for linux 集群环境 2.使用vs2017创建一个web api应用程序,并 ...
- 年末展望:Oracle 对 JDK收费和.NET Core 给我们的机遇
2018年就结束了,马上就要迎来2019年,这一年很不平凡,中美贸易战还在继续,IT互联网发生急剧变化,大量互联网公司开始裁员,微软的市值在不断上升 ,在互联网公司的市值下跌过程中爬到了第一的位置,我 ...
- 后端MVC和前端MVVC关系详解
MVC 是后端的分层开发概念: MVVM是前端视图层的概念,主要关注于 视图层分离,也就是说:MVVM把前端的视图层,分为了 三部分 Model, View , VM ViewModel
- 数组属性的习题、Arrays工具、二维数组
一.数组的练习 1.声明一个char类型的数组, 从键盘录入6个字符: [1]遍历输出 [2]排序 [3]把char数组转化成一个逆序的数组. import java.util.Scanner; pu ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之三 || Swagger的使用 3.1
常见问题 1.经常有小伙伴遇到这个错误 这是因为接口json文档定义和调用不是一个 1.定义: ConfigureServices 方法中的 services.AddSwaggerGen 注册的一个 ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十二║Vue实战:个人博客第一版(axios+router)
前言 今天正式开始写代码了,之前铺垫了很多了,包括 6 篇基础文章,一篇正式环境搭建,就是为了今天做准备,想温习的小伙伴可以再看看<Vue 基础入门+详细的环境搭建>,内容很多,这里就暂时 ...
- android学习笔记--检测是否为wifi环境
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); // 获取 ...
- Xcode 10 iOS12 "A valid provisioning profile for this executable was not found
问题:Xcode10 build 没问题,跑虚拟机也没问题,数据线连上run到真机上出现 Xcode 10 iOS12 "A valid provisioning profile for t ...