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. c++实现 给定直角停车位两个点,求取剩余两点坐标。

    //2018-09-08-fourmi /*************************include head files************************************ ...

  2. 滴水穿石-07Java开发中常见的错误

    1:使用工具Eclipse 1.1 "语法错误" 仅当源级别为 1.5 时已参数化的类型才可用 设置eclipse,窗口—>java—>编译器—>JDK一致性调到 ...

  3. MVC开发中的常见错误-01未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。

    错误信息:未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或 ...

  4. IEDA序列化设置

  5. linux基础练习题(2)

    Linux命令作业(关卡二) 练习题1 理解操作系统的作用,以及各种操作系统的不同 要求: 为什么要有OS?没有OS能行吗?原因是什么? Linux内核指的是什么? Linux主要应用在哪些地方? 使 ...

  6. 用JAVA写一个简单的英文加密器

    package qhs; import java.util.Scanner; public class JiaM { public static void main(String[] args) { ...

  7. dubbo支持哪些通信协议和序列化协议

    dubbo支持的通信协议 dubbo协议 dubbo://192.168.0.1:20188 默认就是走dubbo协议的,单一长连接,NIO异步通信,基于hessian作为序列化协议 适用的场景就是: ...

  8. jmeter4.x centos7部署笔记

    1. jmeter依赖 java8或以上版本 安装 java : 参考  https://tecadmin.net/install-java-8-on-centos-rhel-and-fedora/ ...

  9. [转]fiddler 抓包 HTTPS 请求

    教程开始 安装 fiddler 首先准备一台可以上网的 windos 电脑,准备一部智能手机. fiddler 抓包工具:下载地址( 自行百度一搜一大片).安装,打开如果遇到. net framewo ...

  10. nginx安装,运行(ubuntu)

    文本只涉及单节点nginx 安装gcc g++依赖库 apt-get install build-essential apt-get install libtool 安装pcre依赖库 apt-get ...