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. CHENGDU3-Restful API 接口规范、django-rest-framework框架

    Restful API 接口规范.django-rest-framework框架 问题:什么是API? 答:API是接口,提供url. 接口有两个用途: 为别人提供服务,前后端分离. 为什么使用前后端 ...

  2. C++设计模式——观察者模式(转)

    前言 之前做了一个性能测试的项目,就是需要对现在的产品进行性能测试,获得测试数据,然后书写测试报告,并提出合理化的改善意见.项目很简单,我们获得了一系列性能测试数据,对于数据,我们需要在Excel中制 ...

  3. Python算法之二分查找法

    1: l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88] 从列表中找到某个num的位置 def ...

  4. 一脸懵逼学习Hdfs---动态增加节点和副本数量管理(Hdfs动态扩容)

    1:按照上篇博客写的,将各个进程都启动起来: 集群规划:    主机名        IP                安装的软件                    运行的进程   master ...

  5. hihocoder 前两题思路

    1800 : 玩具设计师 二维前缀和的写法有很多,最常见的是s[x-1][y]+s[x][y-1]-s[x-1][y-1]+a[x][y]; 涉及二维矩阵求和,联想前缀和,求>=指定面积的最大耐 ...

  6. PyTorch中的backward [转]

    转自:https://sherlockliao.github.io/2017/07/10/backward/ backward只能被应用在一个标量上,也就是一个一维tensor,或者传入跟变量相关的梯 ...

  7. 在CentOS 7+ 安装Kubernetes入门(单Master)

    TL;DR; ***,***,***,重要的事情说三次.如果不会***,这篇文章就没有看下去的意义.作为一个技术人员如果不愿意折腾,很难有所作为.作为一个单纯的技术人员,最好把心思放在技术上,做到真正 ...

  8. (二)Makefile——自动编译、清理、安装软件

    每次都要敲击冗长的编译命令,每次都要清理之前的编译中间结果和最终结果,在安装软件时复制剪切大量的动态库,在卸载软件时删除很多的动态库和配置文件.好吧,我被逼向了makefile. helloworld ...

  9. net core体系-web应用程序-4net core2.0大白话带你入门-5asp.net core环境变量详解

    asp.net core环境变量详解   环境变量详解 Windows操作系统的环境变量在哪设置应该都知道了. Linux(centos版本)的环境变量在/etc/profile里面进行设置.用户级的 ...

  10. 利用Jmeter做接口测试的时候,如何提取头部的JSESSIONID然后传递到下一个请求,继续完成当前用户的请求。

    其实,关于这个问题有三种种解决方法: 1)从响应数据里面提取JSESSIONID,点击链接可以查看https://www.cnblogs.com/liulinghua90/p/5320290.html ...