Luogu 3690 Link Cut Tree

  • \(LCT\) 模板题.可以参考讲解和这份码风(个人认为)良好的代码.
  • 注意用 \(set\) 来维护实际图中两点是否有直接连边,否则无脑 \(Link/Cut\) 会崩掉.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{
int x=0;
bool pos=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
pos=0;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
return pos?x:-x;
}
const int MAXN=3e5+10;
struct node{
int val,sum,rev;
int ch[2],fa;
node()
{
rev=0;
val=sum=0;
}
}tree[MAXN];//splay以原图中的深度为关键字.
set<int> Link[MAXN];//维护两点是否有边相连
int n,m,stk[MAXN],tp=0;
#define root tree[x]
#define lson tree[tree[x].ch[0]]
#define rson tree[tree[x].ch[1]]
void pushup(int x)
{
root.sum=lson.sum^root.val^rson.sum;
}
void inverse(int x)
{
swap(tree[x].ch[0],tree[x].ch[1]);
root.rev^=1;
}
void pushdown(int x)
{
if(root.rev)
{
if(root.ch[0])
inverse(root.ch[0]);
if(root.ch[1])
inverse(root.ch[1]);
root.rev=0;
}
}
bool isroot(int x)
{
return tree[root.fa].ch[0]!=x && tree[root.fa].ch[1]!=x;
}
void rotate(int x)
{
int y=tree[x].fa,z=tree[y].fa;
int k=tree[y].ch[1]==x;
if(!isroot(y))
tree[z].ch[tree[z].ch[1]==y]=x;
tree[x].fa=z;//splay中,根的父亲是原图中链顶(该节点)的父亲
tree[y].ch[k]=tree[x].ch[k^1];
tree[tree[x].ch[k^1]].fa=y;
tree[x].ch[k^1]=y;
tree[y].fa=x;
pushup(y);
}
void splay(int x)
{
stk[++tp]=x;
for(int pos=x;!isroot(pos);pos=tree[pos].fa)
stk[++tp]=tree[pos].fa;
while(tp)
pushdown(stk[tp--]);//因为要从下往上转,所以先将从x到splay根节点上的标记全部下传
while(!isroot(x))
{
int y=tree[x].fa,z=tree[y].fa;
if(!isroot(y))
(tree[y].ch[0]==x)^(tree[z].ch[0]==y)?rotate(x):rotate(y);
rotate(x);
}
pushup(x);
}
void Access(int x)
{
for(int y=0;x;y=x,x=tree[x].fa)
{
splay(x);
tree[x].ch[1]=y;
pushup(x);
}
}
void makeroot(int x)
{
Access(x);
splay(x);
inverse(x);
}
int findroot(int x)
{
Access(x);
splay(x);//翻转后找深度最小的,就是原来深度最大的.
while(tree[x].ch[0])
x=tree[x].ch[0];
return x;
}
void split(int x,int y)
{
makeroot(x);
Access(y);//x到y是一颗splay
splay(y);//然后就是y及其左子树存储的信息了. (实际上就是y,因为它没有右子树)
}
void link(int x,int y)
{
if(Link[x].find(y)!=Link[x].end())
return;
makeroot(x);
tree[x].fa=y;//连一条虚边
Link[x].insert(y);
Link[y].insert(x);
}
void cut(int x,int y)
{
if(Link[x].find(y)==Link[x].end())
return;
split(x,y);//在splay中从y一直往左走到x.
tree[y].ch[0]=0;
tree[x].fa=0;
Link[x].erase(y);
Link[y].erase(x);
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;++i)
tree[i].val=tree[i].sum=read();//原图编号即splay标号
while(m--)
{
int op=read(),x=read(),y=read();
if(op==0)
{
split(x,y);
printf("%d\n",tree[y].sum);
}
else if(op==1)
{
link(x,y);
}
else if(op==2)
{
cut(x,y);
}
else
{
Access(x);
splay(x);//这样不用修改splay中其他节点信息
tree[x].val=y;
pushup(x);
}
}
return 0;
}

Luogu 3690 Link Cut Tree的更多相关文章

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

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

  2. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  3. 洛谷P3690 [模板] Link Cut Tree [LCT]

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

  4. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  5. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  6. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

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

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

  8. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  9. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

随机推荐

  1. 锁(3)-- DB锁

    1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一 ...

  2. Partition List,拆分链表

    问题描述: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  3. How to implement multiple constructor with different parameters in Scala

    Using scala is just another road, and it just like we fall in love again, but there is some pain you ...

  4. jQuery实际案例②——三层轮播图

    1.如图,这种轮播图需要实现的是,当鼠标移到2上时,第二张图片从右侧过来 2.需要注意的:①很明显这是通过改变z-index与left值来实现的:  ②整体布局需注意,图与数值(1,2,3,4,5)两 ...

  5. Android6.0------权限管理

    此博客主要谈谈Android6.0的权限,关于其他6.0的知识 请看https://developer.android.com/about/versions/marshmallow/android-6 ...

  6. Python 字典的一键多值,即一个键对应多个值

    转自:http://blog.csdn.net/houyj1986/article/details/22624981 #encoding=utf-8 print '中国' #字典的一键多值 print ...

  7. confluence wiki 破解安装操作流程

    准备postgres数据库安装 步骤1:命令: docker pull postgres 步骤2:安装: docker run --name postgresdb -p 5432:5432 -e PO ...

  8. IOS-CocoaPods进阶篇

    一.Podfile.lock文件 上文讲过,在开始使用CocoaPods,执行完pod install之后,会生成一个Podfile.lock文件.这个文件看起来跟我们关系不大,实际上绝对不应该忽略它 ...

  9. hdu2732

    题解: 和上一题差不多 然后注意格式 代码: #include<cstdio> #include<cmath> #include<algorithm> #inclu ...

  10. (转)Mac os x 下配置Intellij IDEA + Tomcat 出现权限问题的解决办法

    出现的错误提示如下: 下午9:11:27 All files are up-to-date下午9:11:27 All files are up-to-date下午9:11:27 Error runni ...