题意:

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

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题目11613n+1问题

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAIvCAIAAAAXg+GWAAAgAElEQVR4nO3dO1LryNsH4G8T5CyE2A ...

  2. web项目没有run on server时..

    文章转载至:http://blog.csdn.net/hongchangfirst/article/details/7722703 web项目没有run on server 1.首先确保正确安装Tom ...

  3. jdbc连接oracle数据库

    /*** 通过改变配置文件来连接不同数据库*/package com.xykj.jdbc; import static org.junit.Assert.*; import java.io.Input ...

  4. Android -- Looper、Handler、MessageQueue等类之间关系的序列图

    原文:Android源码解析之(二)-->异步消息机制 通过阅读文章及其中提到的一些参考文章,模模糊糊的理解了Android的异步消息机制.为了能够进行消化吸收,决定把各类之间的交互通过图的形式 ...

  5. PHP define()的用法

    define()函数理解1(着重于作用的理解) define() 函数定义一个常量. 常量的特点: 常量类似变量,不同之处在于:在设定以后,常量的值无法更改常量名,不需要开头的美元符号 ($),作用域 ...

  6. Python 打包程序判断是否已经运行

    代码如下: # -*- coding: UTF8 -*- from win32com.client import Dispatch import win32com import sys, os fro ...

  7. 第二十三篇:在SOUI中使用LUA脚本开发界面

    像写网页一样做客户端界面可能是很多客户端开发的理想. 做好一个可以实现和用户交互的动态网页应该包含两个部分:使用html做网页的布局,使用脚本如vbscript,javascript做用户交互的逻辑. ...

  8. 学习设计接口api(转)

    介绍 先说说啥是 Api 吧,以下摘自百度百科: API (Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于 ...

  9. JS 获取元素当前的样式信息

    HTMLElement.prototype.__defineGetter__("currentStyle", function () { return this.ownerDocu ...

  10. 2016 ACM/ICPC Asia Regional Dalian Online HDU 5877 Weak Pair treap + dfs序

    Weak Pair Problem Description   You are given a rooted tree of N nodes, labeled from 1 to N. To the  ...