Codeforces Round #541 F. Asya And Kittens
题面:
题目描述:
题目分析:



所以问题来了,第一:对于合并3,1我怎么知道是要合并3和1,4,而不是只合并3和1,把4抛弃?这时就要用到并查集,我们可以写一个并查集,查什么呢?当然是查在哪个隔间,然后把两个隔间合并。第二:这个好像只是模拟这个过程啊,怎样保存结果?其中一种做法就是给隔间分配额外的编号,把这个树(记得存树是存树的编号)存下来,然后用递归遍历一次就可以输出结果了(记得这时并查集查的是隔间的编号)。树:

1 #include <bits/stdc++.h> //万能头文件
2 using namespace std;
3 const int maxn = 150000 + 5;
4 int n;
5 int sets[2*maxn], L_node[2*maxn], R_node[2*maxn];
6
7 int F(int x){ //查
8 if(sets[x] == x || !sets[x]) return x;
9 return sets[x] = F(sets[x]);
10 }
11
12 void print(int x){ //输出答案
13 if(x <= n) printf("%d ", x);
14 else {
15 print(L_node[x]);
16 print(R_node[x]);
17 }
18 }
19
20 int main(){
21 cin >> n;
22 int id = n; //隔间编号
23 int x, y;
24 for(int i = 0; i < n-1; i++){
25 scanf("%d%d", &x, &y);
26 x = F(x); //查
27 y = F(y);
28 sets[x] = sets[y] = ++id; //并
29 L_node[id] = x; //记录在哪个隔间
30 R_node[id] = y;
31 }
32 print(id);
33 return 0;
34 }
大佬的代码:(好像用数组模拟链表实现)
1 #include <bits/stdc++.h> //万能头文件
2 using namespace std;
3 const int maxn = 150000 + 5;
4 int n;
5 int sets[maxn], G[maxn], last[maxn];
6
7 int F(int x){ //查
8 if(sets[x] == x) return x;
9 return sets[x] = F(sets[x]);
10 }
11
12 int main(){
13 cin >> n;
14 int x, y;
15
16 //初始化
17 for(int i = 1; i <= n; i++){
18 sets[i] = i;
19 last[i] = i;
20 }
21
22 for(int i = 0; i < n-1; i++){
23 scanf("%d%d", &x, &y);
24 x = F(x);
25 y = F(y);
26 G[last[x]] = y; //x的末尾一个元素接y
27 last[x] = last[y]; //x的末尾一个元素更新为y的末尾一个元素
28 sets[y] = x; //并, 且x为代表元
29 }
30
31 for(int i = F(1); i; i = G[i]){
32 scanf("%d ", i);
33 }
34 return 0;
35 }
Codeforces Round #541 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 ...
- 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 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #541 (Div. 2) (A~F)
目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...
- Codeforces Round #541 (Div. 2)
Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...
- Codeforces Round #541
因为这次难得不在十点半(或是更晚),大家都在打,然后我又双叒叕垫底了=.= 自己对时间的分配,做题的方法和心态还是太蒻了,写的时候经常写一半推倒重来.还有也许不是自己写不出来,而是在开始写之前就觉得自 ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- F. Asya And Kittens并查集
F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- 实现基于股票收盘价的时间序列的统计(用Python实现)
时间序列是按时间顺序的一组真实的数字,比如股票的交易数据.通过分析时间序列,能挖掘出这组序列背后包含的规律,从而有效地预测未来的数据.在这部分里,将讲述基于时间序列的常用统计方法. 1 用rollin ...
- 【DP】区间DP入门
在开始之前我要感谢y总,是他精彩的讲解才让我对区间DP有较深的认识. 简介 一般是线性结构上的对区间进行求解最值,计数的动态规划.大致思路是枚举断点,然后对断点两边求取最优解,然后进行合并从而得解. ...
- Linux 驱动框架---linux 驱动
总述 Linux 系统下的驱动最后都是以如下这个结构体呈现在系统中的,注意其中的dev_pm_ops是内核新增的内容来准备替换platform_driver中的电源管理相关的内容.这里内容是先进行总体 ...
- js console API All In One
js console API All In One const log = console.log; for(const key in console) { log(`navigator.${key} ...
- rename github
rename GitHub github repo rename xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
- mongodb & vue & node.js
mongodb mongodb & vue & node.js https://docs.mongodb.com/manual/tutorial/install-mongodb-on- ...
- js & Event Bus
js & Event Bus global event handler (broadcast / trigger / emit / listen ) // 实现一个 EventBus类,这个类 ...
- css icons fontawesome-free
官网 examples v4.7.0 cdnjs icons basic-use 安装 λ npm install --save @fortawesome/fontawesome-free fa前缀在 ...
- Dapr 知多少 | 分布式应用运行时
Intro Dapr 官方团队已于最近(2021.1.17)正式发布Dapr v1.0,Dapr已正式生产可用,可以部署到自托管环境或 Kubernetes 集群.对于绝大多数开发者来说,想必对Dap ...
- 怎么创建CSV文件和怎么打开CSV文件
CSV(Comma Separated Values逗号分隔值). .csv是一种文件格式(如.txt..doc等),也可理解.csv文件就是一种特殊格式的纯文本文件.即是一组字符序列,字符之间已英文 ...