Luogu 3690 Link Cut Tree
Luogu 3690 Link Cut Tree
#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的更多相关文章
- (RE) luogu P3690 【模板】Link Cut Tree
二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...
- Link Cut Tree 总结
Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
随机推荐
- 如何利用mixin编写media query的代码
mixins允许文档作者定义的属性对时可以在其他规则集中重用的模式. Media Queries直译就是“媒体查询”.media就是来指定特定的媒体类型,如屏幕(screen),或者“TV”等,其中“ ...
- hdu 5696 区间的价值 单调栈+rmq
区间的价值 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- kubernetes源码分析 -- kube-proxy
Kube-proxy需要在每一个minion结点上运行.他的作用是service的代理,负责将业务连接到service后面具体执行结点(endpoints). 我们列一下体现kube-proxy ...
- spring mvc: 资源绑定视图解析器(不推荐)
spring mvc: 资源绑定视图解析器(不推荐) 不适合单控制器多方法访问,有知道的兄弟能否告知. 访问地址: http://localhost:8080/guga2/hello/index 项目 ...
- Python之NumPy(axis=0 与axis=1)区分
转自:http://blog.csdn.net/wangying19911991/article/details/73928172 https://www.zhihu.com/question/589 ...
- UVALive-5135 Mining Your Own Business (无向图的双连通分量)
题目分析:在一张无向图中,将一些点涂上黑色,使得删掉图中任何一个点时,每个连通分量至少有一个黑点.问最少能涂几个黑点,并且在涂最少的情况下有几种方案. 题目分析:显然,一定不能涂割点.对于每一个连通分 ...
- springboo 添加logback日志
springboot默认引入logback相关的jar包 1.在 Application.properties里添加 logging.config=classpath:logback-spring.x ...
- eclipse安装插件:
eclipse安装插件:jre跟eclipse的bit数必须匹配,即必须都是32or64位的 历史版本不好找,pydev的历史版本在sourceforge中很隐蔽,得在项目的activite中查找,另 ...
- 高射炮打蚊子丨用Visual Studio 2017写最初级的C语言程序
众所周知,Visual Studio号称全宇宙最强的IDE(集成开发环境),简直可以“秒天秒地秒空气”.我们看着各种技术大会上,大神们在台上用VS演示Demo溜得飞起,然而对于一些非技术专业同学或者是 ...
- 抽奖小程序,js,canvas
js写的网页抽奖小程序,先上截图 源码地址:https://github.com/xiachaoxulu/raffle