网上一片一片的LCT都是数组写的 orz

用指针写splay的人想用指针写LCT找板子都不好找QAQ

所以能A题了之后自然要来回报社会, 把自己的板子丢上来(然而根本没有人会看)

LCT讲解就省省吧, 我这种蒟蒻也基本讲不清楚, 就扔个板子算了

板子里也没什么注释, 因为函数的命名和世界的主流命名规范是一样的..

那就这样吧.. 贴两道题的板子.

第一个是SDOI2008 Cave洞穴勘测, 一道简单判断连通性的LCT裸题.

三种操作;

  1. 连x,y 保证连完还是棵树
  2. 断x,y 保证<x,y>边存在
  3. 询问x,y是否联通

    然而据说用不路径压缩的并查集可过(考场上我估计就写这个了

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=20101;
inline int gn(int a=0,char c=0){
for(;c<'0'||c>'9';c=getchar());
for(;c>47&&c<58;c=getchar())a=a*10+c-48;return a;
}
struct node{
node *fa,*ch[2];
bool rev;
bool getwh();
bool isroot();
void pushdown();
void setch(bool wh,node* child);
}pool[N],*null; int tot;
bool node::getwh(){
return fa->ch[1]==this;
}
bool node::isroot(){
return fa==null||(fa->ch[0]!=this&&fa->ch[1]!=this);
}
void node::pushdown(){
if(null==this||!rev) return;
swap(ch[0],ch[1]);
ch[0]->rev^=1; ch[1]->rev^=1;
rev=0;
}
void node::setch(bool wh,node* child){
pushdown(); ch[wh]=child;
if(null!=child) child->fa=this;
}
void init(){
null=pool; null->rev=0;
null->fa=null->ch[0]=null->ch[1]=null;
}
node* newnode(){
node* x=pool+ ++tot; x->rev=0;
x->ch[0]=x->ch[1]=x->fa=null;
return x;
}
void rotat(node* x){
node *fa=x->fa,*fafa=fa->fa;
if(fafa!=null) fafa->pushdown();
fa->pushdown(); x->pushdown();
int wh=x->getwh();
if(fa->isroot()) x->fa=fa->fa;
else fafa->setch(fa->getwh(),x);
fa->setch(wh,x->ch[wh^1]);
x->setch(wh^1,fa);
}
void fix(node* x){
if(!x->isroot()) fix(x->fa);
x->pushdown();
}
void splay(node* x){
fix(x);
for(;!x->isroot();rotat(x))
if(!x->fa->isroot())
x->getwh()==x->fa->getwh()?rotat(x->fa):rotat(x);
}
node* access(node* x){
node* y=null;
for(;x!=null;x=x->fa){
splay(x); x->ch[1]=y; y=x;
}
return y;
}
void makeroot(node* x){
access(x)->rev^=1;
splay(x);
}
void link(node* x,node* y){
makeroot(x); x->fa=y;
}
void cut(node* x,node* y){
makeroot(x); access(y); splay(y);
x->fa=y->ch[0]=null;
}
node* Find(node* x){
for(access(x),splay(x);x->ch[0]!=null;x->pushdown(),x=x->ch[0]);
return x;
}
int main(){ init();
int n=gn(),m=gn();
for(int i=1;i<=n;++i) newnode();
char opt[123];
for(int i=1;i<=m;++i){
scanf("%s",opt);
int x=gn(),y=gn();
node *X=pool+x,*Y=pool+y;
if(opt[0]=='Q')
puts(Find(X)==Find(Y)?"Yes":"No");
else if(opt[0]=='C')
link(X,Y);
else cut(X,Y);
}
}

然后另一道是luogu的模板题, 这个是带点权的

(如果带边权的话还是化边为点...)

四种操作:

  1. 查询x,y路径的异或和
  2. 连x,y 已连通则忽略
  3. 断x,y 不保证x,y间有边
  4. 单点修改

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=300020;
inline int gn(int a=0,char c=0){
for(;c<'0'||c>'9';c=getchar());
for(;c>47&&c<58;c=getchar())a=a*10+c-48;return a;
}
struct node{
node *fa,*ch[2];
bool rev; int xum,xx;
bool getwh(){return fa->ch[1]==this;}
bool isroot();
void update(){
xum=ch[0]->xum^ch[1]->xum^xx;
}
void pushdown();
void setch(bool wh,node* child);
}pool[N],*null;
int tot;
bool node::isroot(){
return fa==null||(fa->ch[0]!=this&&fa->ch[1]!=this);
}
void node::pushdown(){
if(this==null||!rev) return;
swap(ch[0],ch[1]);
ch[0]->rev^=1;
ch[1]->rev^=1;
rev=0;
}
void node::setch(bool wh,node* child){
pushdown(); ch[wh]=child;
if(child!=null) child->fa=this;
}
void init(){
null=pool; null->ch[0]=null->ch[1]=null->fa=null; null->rev=0;
}
node* newnode(int val){
node* x=pool+ ++tot; x->xx=val;
x->ch[0]=x->ch[1]=x->fa=null;
x->rev=0; return x;
}
void rotat(node* x){
node *fa=x->fa,*fafa=fa->fa;
if(!fa->isroot()) fafa->pushdown();
fa->pushdown(); x->pushdown();
int wh=x->getwh();
fa->setch(wh,x->ch[wh^1]);
if(fa->isroot()) x->fa=fa->fa;
else fafa->setch(fa->getwh(),x);
x->setch(wh^1,fa);
fa->update(); x->update();
}
void fix(node* x){
if(!x->isroot()) fix(x->fa);
x->pushdown();
}
void splay(node *x){
fix(x);
for(;!x->isroot();rotat(x))
if(!x->fa->isroot())
x->getwh()==x->fa->getwh()?rotat(x->fa):rotat(x);
x->update();
}
node* access(node* x){
node* y=null;
for(;x!=null;x=x->fa){
splay(x);
x->ch[1]=y;
x->update();
y=x;
}
return y;
}
void makeroot(node* x){
access(x)->rev=1; splay(x);
}
void link(node* x,node* y){
makeroot(x);
x->fa=y;
// access(x);
}
void cut(node* x,node* y){
makeroot(x); access(y); splay(y);
x->fa=y->ch[0]=null;
}
node* Find(node* x){
for(access(x),splay(x);x->ch[0]!=null;x->pushdown(),x=x->ch[0]);
return x;
}
void change(node* x,int y){
access(x); splay(x);
x->xx=y; x->update();
}
int query(node* x,node* y){
makeroot(x);
access(y);
splay(y);
return y->xum;
}
int main(){
init();
int n=gn(),m=gn();
for(int i=1;i<=n;++i) newnode(gn());
for(int i=1;i<=m;++i){
int opt=gn(),x=gn(),y=gn();
node *X=pool+x,*Y=opt==3?null:pool+y;
switch(opt){
case 0:
printf("%d\n",query(X,Y));
break;
case 1:
if(Find(X)!=Find(Y))
link(X,Y);
break;
case 2:
if(Find(X)==Find(Y))
cut(X,Y);
break;
case 3:
change(X,y);
break;
}
}
}

就这样吧...

【模板篇】Link Cut Tree模板(指针)的更多相关文章

  1. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  2. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  3. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  4. LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测

    UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...

  5. (RE) luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

  6. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  7. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  8. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  9. 指针版P3690 【模板】Link Cut Tree (动态树)

    题面 传送门 题解 鉴于数组版实在是太慢我用指针版重新写了一遍 代码基本是借鉴了lxl某道关于\(LCT\)的题 //minamoto #include<bits/stdc++.h> #d ...

随机推荐

  1. Lock中使用Condition实现等待通知

    Condition类有很好的灵活性,可以实现多路通知功能,一个Lock对象中可以创建多个Condition对象实例,线程对象可以注册在指定的Condition中,进而有选择的进行线程通知,在调度线程上 ...

  2. 【记录】uni-app Chrome跨域解决方案插件 has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is...

    博主最近在用Hbuilder X开发前端网页时, 出现了has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header ...

  3. 2018-8-10-win10-uwp-ping

    title author date CreateTime categories win10 uwp ping lindexi 2018-08-10 19:17:19 +0800 2018-2-13 1 ...

  4. (转)Http和Https的区别

    1.什么是Http Http中文叫做超文本传输协议, 它完成客户端到服务端等一系列运作流程 1.1 与http关系密切的协议: IP, TCP和DNS 负责传输的IP协议 IP协议数据网络层, IP协 ...

  5. ORA-01400: cannot insert NULL into

    Error text: ORA-01400: cannot insert NULL into when insert into view, NULL value handler in trigger. ...

  6. Oracle 拼音码函数

    拼音码 select comm.fun_spellcode('数据库') from dual 结果 : SJK 函数 CREATE OR REPLACE FUNCTION COMM.FUN_SPELL ...

  7. 有穷自动机(NFA、DFA)&正规文法&正规式之间的相互转化构造方法

    在编译原理(第三版清华大学出版社出版)中第三章的词法分析中,3.4.3.5.3.6小节中分别讲解了 1.什么是NFA(不确定的有穷自动机)和DFA(确定的有穷自动机) 2.如何将  不确定的有穷自动机 ...

  8. git常用操作命令归纳

    git开始 全局配置:配置用户名和e-mail地址 $ git config --global user.name"Your Name" $ git config --global ...

  9. python 爬取拉勾网

    import requestsimport randomimport timeimport osimport csvimport pandas as pdreq_url = 'https://www. ...

  10. Boost Download

    { //https://www.boost.org/users/history/version_1_71_0.html }