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的更多相关文章

  1. CF1131F Asya And Kittens(Kruskal重构树,启发式合并)

    这题难度1700,我感觉又小了…… 这题虽然没几个人是用kruskal重构树的思想做的,但是我是,所以我就放了个kruskal重构树的标签. 题目链接:CF原网 题目大意:有一个长为 $n$ 的排列, ...

  2. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  3. codeforces #541 F Asya And Kittens(并查集+输出路径)

    F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...

  4. F. Asya And Kittens并查集

    F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #541 F. Asya And Kittens

    题面: 传送门 题目描述: Asya把N只(从1-N编号)放到笼子里面,笼子是由一行N个隔间组成.两个相邻的隔间有一个隔板. Asya每天观察到有一对想一起玩,然后就会把相邻的隔间中的隔板取出来,使两 ...

  6. F. Asya And Kittens 并查集维护链表

    reference :https://www.cnblogs.com/ZERO-/p/10426473.html

  7. Codeforces 1131F Asya And Kittens (构造)【并查集】

    <题目链接> 题目大意:有$n$只小猫,开始将它们放在指定的n个单元格内,然后随机从n-1个隔板中拆除隔板,最终使得这些小猫在同一单元格.现在依次给出拆除隔板的顺序,比如:1 4 就表示1 ...

  8. Codeforces Round #541--1131F. Asya And Kittens(基础并查集)

    https://codeforces.com/contest/1131/problem/F #include<bits/stdc++.h> using namespace std; int ...

  9. 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 ...

随机推荐

  1. 学习REST

    REST:Representational State Transfer,资源的表现状态转换.可以理解为对资源的操作.   1. 资源 资源就是业务对象,如图片.文本.歌曲或者客户.交易等.这些是用户 ...

  2. python requests的content和text方法的区别

    requests对象的get和post方法都会返回一个Response对象,这个对象里面存的是服务器返回的所有信息,包括响应头,响应状态码等.其中返回的网页部分会存在.content和.text两个对 ...

  3. Vim设计

    像 IDE 一样使用 vim 免费的编程中文书籍索引

  4. js数字货币格式互转

    //将1,234,567.00转换为1234567.00 function moneyToNumValue(val) { var num = val.trim(); var ss = num.toSt ...

  5. python之GIL release (I/O open(file) socket time.sleep)

    0.目录 2.线索 C源代码 Py_BEGIN_ALLOW_THREADS Py_END_ALLOW_THREADS3.open(name[, mode[, buffering]]) -> fi ...

  6. EF 数据版本号,处理具体使用方法 RowVersion / Timestamp 使用方法。进行自动处理并发修改

    /* * <div class="form-group"> // 原始 * <div class="form-group hidden"> ...

  7. Python学习(三十)—— Django框架简介

    转载自:http://www.cnblogs.com/liwenzhou/p/8296964.html Django框架简介 一.MVC框架和MTV框架(了解即可) MVC,全名是Model View ...

  8. Python学习(十三) —— 网络编程

    一.操作系统基础 操作系统(Operating System):OS是管理和控制计算机硬件和软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行 ...

  9. BZOJ4566 [Haoi2016]找相同字符 字符串 SAM

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4566.html 题目传送门 - BZOJ4566 题意 给定两个字符串 $s1$ 和 $s2$ ,问有 ...

  10. asp grid 增加和删除行数据

    <table border="0" cellpadding="0" cellspacing="0" style="width ...