BZOJ 2049 & LCT又一模板
题意:
自己看...加边删边问联通...
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又一模板的更多相关文章
- BZOJ 2049 LCT
思路:LCT的基本操作 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm&g ...
- [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...
- bzoj 2049: [Sdoi]Cave 洞穴探测 (LCT)
第一次写lct (这是一道lct裸题 这次没有可爱(划掉)的同学教我,虽然有模板,但是配合网上的讲解还是看不懂QAQ 然后做了几道题之后总算有些感觉辣 于是决定给自己挖个坑,近期写一个lct详解(不过 ...
- bzoj 2049 [Sdoi2008]Cave 洞穴勘测(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题意] 给定森林,可能有连边或断边的操作,回答若干个连通性的询问. [思路] ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘測 LCT
入门级LCT: 仅仅有 Cut Link 2049: [Sdoi2008]Cave 洞穴勘測 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 3073 ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- BZOJ 2049 SDOI2008 洞穴勘测 LCT板子
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
随机推荐
- 微信支付 - V3退款
退款问题: 1.证书加载不进去,出现"内部错误" 解决:在iis中找到对应的应用连接池,右键高级设置,找到"加载用户配置文件"改为true. 2.需 ...
- VS对路径的访问被拒绝
问题:权限问题. 1.检查自己电脑账户.是否是Administrator.如果没有启用.在[我的电脑]——[管理]——[本地用户和组]——[用户]——[Administrator]——[属性启用] 2 ...
- Android5.0如何正确启用isLoggable(二) 理分析
转自:http://www.it165.net/pro/html/201506/43374.html 概要 在上文<Android 5.0 如何正确启用isLoggable(一)__使用详解&g ...
- repo 版本回退
转自:http://blog.csdn.net/wed110/article/details/52179386 1.repo 回退到具体某一天的提交 repo forall -c 'ID=`Git l ...
- Memcached缓存在.Net 中的使用(memcacheddotnet)
缓存对于提高大数据量的网站性能无疑不是一个很好的解决方案,针对缓存的使用网上同仁介绍很多,再次我仅仅分享一下自己对Memcached使用的简单介绍.Memchached的使用通过第三方DLL来完成,常 ...
- Java代码实现excel数据导入到Oracle
1.首先需要两个jar包jxl.jar,ojdbc.jar(注意版本,版本不合适会报版本错误)2.代码: Java代码 import java.io.File; import java.io.Fi ...
- php获取文件夹下面的文件列表和文件夹列表
function getDir($dir) { $dirArray[] = NULL; if (false != ($handle = opendir( $dir ))) { $i=0; while ...
- jquery获取radio和select选中值
//jquery 获取radio选中值 <input type="radio" name="c_type" value="a" > ...
- 【leetcode】plus One
问题描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...
- DateTime还是DateTimeOffset?Now还是UtcNow?
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:新年第一篇文章,就来谈谈关于时间的简单技术问题:该用DateTime还是DateTim ...