题意:

  自己看...加边删边问联通...

SOL:

  就加了一个findroot而已...

  然而时间还是惨不忍睹...优化全开也才1700ms...膜seter...

Code:

  

/*==========================================================================
# Last modified: 2016-03-17 18:33
# Filename: 2049.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 100000
#define maxm 100000
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
int ch[maxn][2],rev[maxn],nxt[maxn],fa[maxn],pre[maxn],s[maxn];
int sta[maxn];
int n,m;
inline void pushup(int x){
s[x]=1+s[ch[x][0]]+s[ch[x][1]];
}
inline void pushdown(int x){
if (rev[x]){
swap(ch[x][0],ch[x][1]);
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
rev[x]=0;
}
}
inline void setx(int x){
if (ch[x][1]) {
fa[ch[x][1]]=0; pre[ch[x][1]]=x; ch[x][1]=0; pushup(x);
}
}
inline void rotate(int x){
int p=fa[x],q=fa[p],d=ch[p][1]==x;
fa[ch[p][d]=ch[x][d^1]]=p; pushup(p);
fa[ch[x][d^1]=p]=x; pushup(x);
fa[x]=q;
if (q){
if (ch[q][1]==p) ch[q][1]=x;
else ch[q][0]=x;
}
else pre[x]=pre[p];
}
inline void splay(int x){
int top=0;
for (int i=x;i;i=fa[i]) sta[++top]=i;
for (;top;top--) pushdown(sta[top]);
for (int y;(y=fa[x])!=0;rotate(x))
if (fa[y]) rotate((getlc(y)==x)==(getlc(fa[y])==y)?y:x);
}
inline void access(int x){
splay(x); setx(x); int v;
while(v=pre[x]){
splay(v); setx(v);
ch[v][1]=x; fa[x]=v; pushup(v);
x=v;
}
}
inline void beroot(int x){
access(x);
splay(x);
rev[x]^=1;
}
inline void link(int x,int y){
beroot(x);
pre[x]=y;
//access(x); splay(x);
}
inline void cut(int x,int y){
beroot(x); access(y); splay(y);
ch[y][0]=fa[x]=pre[x]=0;
pushup(y);
}
inline int findroot(int x){
while (ch[x][0]) x=ch[x][0];
return x;
}
inline void query(int x,int y){
beroot(y);
access(x);
splay(x);
if (findroot(x)==y) printf("Yes\n");
else printf("No\n");
}
int main(){
read(n); read(m);
FORP(i,1,n) s[i]=1;
FORP(i,1,m){
char temp[10]; int x,y;
scanf("%s",temp); read(x); read(y);
if (x<y) swap(x,y);
if (temp[0]=='Q') query(x,y);
else if (temp[0]=='C') link(x,y);
else cut(x,y);
}
}

BZOJ 2049 & LCT又一模板的更多相关文章

  1. BZOJ 2049 LCT

    思路:LCT的基本操作 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm&g ...

  2. [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】

    题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...

  3. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...

  4. bzoj 2049: [Sdoi]Cave 洞穴探测 (LCT)

    第一次写lct (这是一道lct裸题 这次没有可爱(划掉)的同学教我,虽然有模板,但是配合网上的讲解还是看不懂QAQ 然后做了几道题之后总算有些感觉辣 于是决定给自己挖个坑,近期写一个lct详解(不过 ...

  5. bzoj 2049 [Sdoi2008]Cave 洞穴勘测(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题意] 给定森林,可能有连边或断边的操作,回答若干个连通性的询问. [思路] ...

  6. BZOJ 2049: [Sdoi2008]Cave 洞穴勘測 LCT

    入门级LCT: 仅仅有 Cut Link 2049: [Sdoi2008]Cave 洞穴勘測 Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 3073 ...

  7. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...

  8. BZOJ 2049 SDOI2008 洞穴勘测 LCT板子

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...

  9. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

随机推荐

  1. NYOJ题目170网络的可靠性

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs8AAANvCAIAAACte6C6AAAgAElEQVR4nOydPbLcNhOu7yaUayGOZy

  2. js 闭包原理理解

    问题?什么是js(JavaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 很显然 ...

  3. php 分页

    分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private ...

  4. PHP不同域名cookie共享(单点登录实现原理)

    PHP使用P3P完成COOKIE跨域操作实际实用中,类似的需求有,比如说我们有两个域名,我们想实现在一个域名登录后,能自动完成另一个域名的登录,也就是单点登录(SSO)功能.为了测试的方便,先编辑ho ...

  5. C可变参数函数 实现

    转自:http://blog.csdn.net/weiwangchao_/article/details/4857567 C函数要在程序中用到以下这些宏: void va_start( va_list ...

  6. 10g ASM下修改control file的位置

    1.查看位置以及name是否正确 SQL> sho parameter name NAME TYPE VALUE ------------------------------------ --- ...

  7. 浅学JSON——Json.NET之首次试手

    首次遭遇Json格式,缘由项目中用到Json数据,需要进行解析,为此,将Json数据转为了自己较为熟悉的DataTable格式,以此展示至DataGridView中,验证是否成功. 直接上代码: // ...

  8. 建造者模式与原型模式/builder模式与prototype模式/创建型模式

    建造者模式 定义 用于简化复杂对象的创建 JDK中的建造者模式 java.lang.StringBuilder中的append()方法,每次调用后返回修改后的对象本身. public StringBu ...

  9. 学习iOS的网站

    ios开发者 http://www.codeios.com/   cocoachina http://www.cocoachina.com   code4app http://code4app.com ...

  10. Android中log使用方法

    private static final String ACTIVITY_TAG="MainActivity"; Log.v(MainActivity.ACTIVITY_TAG, ...