[CF1131F] Asya And Kittens
Description:
给定n个点的序列,一开始有n个块,每次将两个块合并,并告诉你这两个块中的一对元素,求一种可能的原序列
Hint:
\(n \le 1.5*10^5\)
Solution:
实在是SB题
考虑把每对点的祖先连上一个虚点,用并查集维护,最后dfs所得的树就行
为什么是对的,因为这棵树会按时间顺序由下往上合并节点
好像还有一种做法,对每个块的祖先维护一个vector表示顺序,合并时启发式合并,复杂度\(O(nlogn)\)
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ls p<<1
#define rs p<<1|1
using namespace std;
typedef long long ll;
const int mxn=1e6+5; //空间开大点
int n,hd[mxn],f[mxn],p,cnt;
inline int read() {
char c=getchar(); int x=0,f=1;
while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
while(c<='9'&&c>='0') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
return x*f;
}
inline int chkmax(int &x,int y) {if(x<y) x=y;}
inline int chkmin(int &x,int y) {if(x>y) x=y;}
struct ed {
int to,nxt;
}t[mxn<<1];
inline void add(int u,int v) {
t[++cnt]=(ed) {v,hd[u]}; hd[u]=cnt;
}
inline int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
void dfs(int u)
{
if(u<=n) printf("%d ",u);
for(int i=hd[u];i;i=t[i].nxt) {
int v=t[i].to;
dfs(v);
}
}
int main()
{
n=read(); p=n; int u,v,x,y;
for(int i=1;i<=mxn-2;++i) f[i]=i;
for(int i=1;i<n;++i) {
u=read(); v=read();
x=find(u),y=find(v);
f[x]=f[y]=++p;
add(p,x); add(p,y);
}
dfs(p);
return 0;
}
[CF1131F] Asya And Kittens的更多相关文章
- CF1131F Asya And Kittens(Kruskal重构树,启发式合并)
这题难度1700,我感觉又小了…… 这题虽然没几个人是用kruskal重构树的思想做的,但是我是,所以我就放了个kruskal重构树的标签. 题目链接:CF原网 题目大意:有一个长为 $n$ 的排列, ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- 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 ...
- Codeforces Round #541 F. Asya And Kittens
题面: 传送门 题目描述: Asya把N只(从1-N编号)放到笼子里面,笼子是由一行N个隔间组成.两个相邻的隔间有一个隔板. Asya每天观察到有一对想一起玩,然后就会把相邻的隔间中的隔板取出来,使两 ...
- F. Asya And Kittens 并查集维护链表
reference :https://www.cnblogs.com/ZERO-/p/10426473.html
- Codeforces 1131F Asya And Kittens (构造)【并查集】
<题目链接> 题目大意:有$n$只小猫,开始将它们放在指定的n个单元格内,然后随机从n-1个隔板中拆除隔板,最终使得这些小猫在同一单元格.现在依次给出拆除隔板的顺序,比如:1 4 就表示1 ...
- Codeforces Round #541--1131F. Asya And Kittens(基础并查集)
https://codeforces.com/contest/1131/problem/F #include<bits/stdc++.h> using namespace std; int ...
- 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 ...
随机推荐
- C++ 内链接 外链接
编译的时候(假如编译器是VS),是以源文件cpp文件为单位,编译成一个个的obj文件,然后再通过链接器把不同的obj文件链接起来.如果一些变量或函数的定义是内连接的话,链接器链接的时候就不会拿它们去与 ...
- 在windows下Apache安装配置
安装,从官网下载,安装即可. 配置遇到一些问题: 1. the requested operation has failed 这是因为安装后的文件目录没有没有写的权限.通过安全设置安装目录的所有 ...
- Red Language
官网地址:http://www.red-lang.org/ 源代码地址:https://github.com/red/red 通过github上的Readme,可以完成Hello World的学习 ...
- k3 Bos开发百问百答
K/3 BOS开发百问百答 (版本:V1.1) K3产品市场部 目录 一.基础资料篇__ 1 [摘要]bos基础资料的显示问题_ 1 [摘要]单 ...
- 解决redis aof文件过大的问题
执行BGREWRITEAOF命令对redis的AOF进行重写 redis-cli BGREWRITEAOF 相关解释: Redis的AOF机制有点类似于Mysql binlog,是Redis的提供的一 ...
- Java集合源码学习(二)ArrayList
1.关于ArrayList ArrayList直接继承AbstractList,实现了List. RandomAccess.Cloneable.Serializable接口,为什么叫"Arr ...
- signal() 和 sigaction()
[摘自<Linux/Unix系统编程手册>] Unix系统提供了两种方式来改变信号处置:signal() 和 sigaction(). signal() 的行为在不同Unix实现间存在差异 ...
- nginx 源码安装的重启命令
源码安装nginx就面临这样的麻烦,不能使用service nginx restart 来重启nginx,没办法只能重新加载下nginx. #/usr/local/nginx/sbin/nginx - ...
- dns-prefetch,新打开页面预抓取
dns-prefetch 对性能提升有多大 转载2016-04-07 12:57:41 标签:网站推广dns-prefetch对性能提 dns-prefetch, 是DNS预获取,也是网页前端的优化的 ...
- NOI2018Day2T1 屠龙勇士 set 扩展欧几里德 中国剩余定理
原文链接https://www.cnblogs.com/zhouzhendong/p/NOI2018Day2T1.html 题目传送门 - 洛谷P4774 题意 题解 首先我们仔细看一看样例可以发现如 ...