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 ...
随机推荐
- 网络安全知识--PHP代码审计/Web For Pantesters 的 XSS
用到 ** WEB FOR Pentester** 注意区分单引号双引号. 常见代码 审计工具 wamp,dwva,zvuldrill,burpsuite,seay源代码审计系统... 1 xss W ...
- canvas绘制五星红旗
代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- HDU 3065 病毒侵袭持续中(AC自动机 模板)题解
题意:给出主串中每个模式串的个数 思路:毒瘤出题人多组数据没说给的是多组数据. 板子: struct Aho{ struct state{ int next[130]; int fail, cnt; ...
- Swift private(set) All In One
Swift private(set) All In One SwiftUI Getters and Setters https://docs.swift.org/swift-book/Language ...
- UTM & User Tracking Message
UTM & User Tracking Message utm_source https://marketingplatform.google.com/about/resources/link ...
- Cookie 政策
Cookie 政策 合规/隐私协议 https://www.synology.cn/zh-cn/company/legal/cookie_policy Cookie Cookie 政策 生效日期:20 ...
- TypeScript 如何编写类库声明文件 .d.ts
TypeScript 如何编写类库声明文件 .d.ts how to write a d.ts file declaration-files/ https://www.typescriptlang.o ...
- 2021 NGK生态所体验好、交易快 引人注目!
据悉,NGK计划于2021年2月15日正式上线自己的生态所(时间待定),目的在于满足NGK生态建设者对于NGK几大币种的交易等需求,如NGK.BGV.SPC.USDN.VAST等.只要上NGK生态所, ...
- NGK生态所即将启程!助力NGK公链建立全方位区块链生态系统!
据NGK官方消息,NGK生态所将暂定于2月15日正式上线.据了解,这是全球首个基于公链打造的生态所,也是NGK生态重要的应用之一. 此前,NGK灵石团队CTO通过多方媒体透露,NGK生态所采用去中心化 ...
- VAST二月上线交易所,打通NGK各大币种之间通道!
1月20日,管理着超过8.7万亿美元资产的全球最大资产管理公司贝莱德似乎已批准其旗下两个相关基金--贝莱德全球分配基金公司和贝莱德基金投资比特币期货.提交给美国证券交易委员会的招股说明书文件显示,贝莱 ...