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 ...
随机推荐
- 生成不带签名(BOM)的UTF8格式的XML
生成XML的一种方法如下: using System.Xml; private void SaveXML(string savePath) { XmlWriterSettings setting = ...
- PAT L2-016. 愿天下有情人都是失散多年的兄妹 (BFS)
L2-016. 愿天下有情人都是失散多年的兄妹 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 呵呵.大家都知道五服以内不得通婚 ...
- Koa 洋葱模型
Koa 洋葱模型 let context = { data: [] }; async function middleware1(ctx, next) { console.log('action 001 ...
- vue农历日历
<template> <div class="calendar-main"> <div class="choose_year"&g ...
- 人物传记:Mila Fletcher:如何勤于思考抓住关键?
Mila Fletcher于2007年毕业于耶鲁大学,她是一名真正意义上的法学博士,在校期间获得了马歇尔奖学金,毕业后曾在美国多家知名律师事务所任职,目前就职于星盟全球投资公司,专注于帮助公司和客户提 ...
- 实现TensorRT-7.0插件自由!(如果不踩坑使用TensorRT插件功能)
本系列为新TensorRT的第一篇,为什么叫新,因为之前已经写了两篇关于TensorRT的文章,是关于TensorRT-5.0版本的.好久没写关于TensorRT的文章了,所幸就以新来开头吧~ 接下来 ...
- Go 去找个对象吧
前言 我的读者中应该大部分都是 Java 从业者,不知道写 Java 这些年是否真的有找到对象? 没找到也没关系,总不能在一棵树上吊死,我们也可以来 Go 这边看看,说不定会有新发现. 开个玩笑,本文 ...
- [计算机图形学]光栅化算法:DDA和Bresenham算法
目录 一.DDA 二.Bresenham 三.绘制图形 1. 绘制直线 2. 绘制圆 3. 绘制椭圆 一.DDA DDA算法是最简单的直线绘制算法.主要思想是利用直线的斜截式:\(y=kx+b\) 对 ...
- JS判断对象是否包含某个属性
1.使用hasOwnProperty()判断 hasOwnProperty方法的参数就是要判断的属性名称,当对象的属性存在时返回true,否则返回false. var obj = { name:'ja ...
- SpringCloud Stream
1.介绍 官网:https://www.springcloud.cc/spring-cloud-dalston.html#_spring_cloud_stream 1.1定义 是一个构建消息驱动微服务 ...