题意:

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

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. selenium--python如何定位一组元素并返回文本值

    from selenium import webdriverimport time a=[] #创建一个空列表用于存储查询到的元素组driver = webdriver.Firefox()driver ...

  2. php 投票系统练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. js获取url参数值(HTML之间传值)

    <h3>未设置设备: <a href="javascript:addTab('设备列表','PKE_DeviceContent/PKE_DeviceContent.aspx ...

  4. wifi display代码 分析

    转自:http://blog.csdn.net/lilian0118/article/details/23168531 这一章中我们来看Wifi Display连接过程的建立,包含P2P的部分和RTS ...

  5. GPS NEMA 0183协议

    转自:http://www.cnblogs.com/xidongs/archive/2011/02/01/1948689.html 一. NMEA0183标准语句(GPS常用语句)$GPGGA例:$G ...

  6. eclipse使用tips-Toggle Mark Occurrences 颜色更改

    Toggle Mark Occurrences这个功能非常好用,能把选中的方法/变量在本类中全部出现的地方高亮显示,是一个非常实用的功能.但是默认颜色是灰色,非常毁眼.可以通过下面的设置更改为自己喜欢 ...

  7. Java简明教程

    Java与C++比较概况 C++ Java class Foo { // 声明 Foo 类 public: int x; // 成员变量 Foo(): x() { // Foo 的构造函数Constr ...

  8. AngularJS讲义-控制器

    在Angular中,控制器(Controller)就是基于JavaScript的构造方法,主要用来构造模型并建立模型和视图之间的数据绑定.控制器里面定义了应用程序的逻辑和行为. 通过ng-contro ...

  9. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  10. js 随机星星 document.createElement(); setAttribute()

    js 随机星星 document.createElement(); setAttribute() <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. ...