题目:https://www.luogu.org/problemnew/show/P3690

自己竟然从没有钻研过LCT上的连通性问题!

于是被最后一个点卡了,似乎因为 find 函数只能找出连通性而不能判断有没有直接相连的边;

所以还是直接在 cut 函数里判断一下好了。

(注:第9个点时T时不T的,不想去管它了。)

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const maxn=3e5+;
int n,m,fa[maxn],c[maxn][],sum[maxn],w[maxn],rev[maxn],sta[maxn],top;
bool isroot(int x){return c[fa[x]][]!=x && c[fa[x]][]!=x;}
void pushup(int x)
{
sum[x]=(sum[c[x][]]^sum[c[x][]]^w[x]);
}
void reverse(int x)
{
if(rev[x])
{
rev[c[x][]]^=; rev[c[x][]]^=; rev[x]=;
swap(c[x][],c[x][]);
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][!d]]=y;
c[y][d]=c[x][!d]; c[x][!d]=y;
pushup(y); pushup(x);
}
void splay(int x)
{
sta[top=]=x;
for(int i=x;!isroot(i);i=fa[i])sta[++top]=fa[i];
for(;top;top--)reverse(sta[top]);
for(;!isroot(x);rotate(x))
{
int y=fa[x],z=fa[y];
if(isroot(y))continue;
((c[y][]==x)^(c[z][]==y))?rotate(x):rotate(y);
}
}
void access(int x)
{
for(int t=;x;c[x][]=t,pushup(x),t=x,x=fa[x])splay(x);
}
void makeroot(int x)
{
access(x); splay(x); rev[x]^=;
}
void link(int x,int y)
{
makeroot(x); fa[x]=y;
}
void query(int x,int y)
{
makeroot(x); access(y); splay(y);
}
void cut(int x,int y)
{
query(x,y);
if(c[y][]!=x)return;//!
fa[x]=; c[y][]=;
}
int find(int x)
{
access(x); splay(x); while(c[x][])x=c[x][]; return x;
}
void change(int u,int t)
{
makeroot(u); w[u]=t; pushup(u);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&w[i]);
for(int i=,op,x,y;i<=m;i++)
{
scanf("%d%d%d",&op,&x,&y);
if(op==){query(x,y); printf("%d\n",sum[y]);}
if(op==)
{
if(find(x)==find(y))continue;
link(x,y);
}
if(op==)
{
// if(find(x)!=find(y))continue;
cut(x,y);
}
if(op==)change(x,y);
}
return ;
}
/*
5 6
1
2
3
4
5
1 1 2
1 2 3
1 3 4
1 4 5
2 1 5
0 1 5 1
*/

洛谷P3690 LCT模板的更多相关文章

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

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

  2. 洛谷 P3690 【模板】Link Cut Tree (动态树) || bzoj 3282: Tree

    https://blog.csdn.net/saramanda/article/details/55253627 https://blog.csdn.net/CHHNZ/article/details ...

  3. 洛谷P3690 【模板】Link Cut Tree (LCT)

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

  4. [洛谷P3690]【模板】Link Cut Tree (动态树)

    题目大意:给定$n$个点以及每个点的权值,要你处理接下来的$m$个操作.操作有$4$种.操作从$0到3编号.点从1到n编号. $0,x,y$:代表询问从$x$到$y$的路径上的点的权值的$xor$和. ...

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

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

  6. 洛谷 P3377 【模板】左偏树(可并堆)

    洛谷 P3377 [模板]左偏树(可并堆) 题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或 ...

  7. 洛谷 P3370 【模板】字符串哈希

    洛谷 P3370 [模板]字符串哈希 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的 ...

  8. 洛谷P3369 【模板】普通平衡树(Treap/SBT)

    洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...

  9. 洛谷P1919 【模板】A*B Problem升级版 题解(FFT的第一次实战)

    洛谷P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 刚学了FFT,我们来刷一道模板题. 题目描述 给定两个长度为 n 的两个十进制数,求它们的乘积. n<=100000 如 ...

随机推荐

  1. cgi fastcgi php-cgi php-fpm

      参考: 摘至:http://www.cnblogs.com/thinksasa/p/4497567.html 详说fastcgi,php-fpm的区别:http://segmentfault.co ...

  2. js 列表几种循环的比较

    数组 遍历 普通遍历 最简单的一种,也是使用频率最高的一种. let arr = ['a', 'b', 'c', 'd', 'e'] for (let i = 0; i < arr.length ...

  3. Inspector's Dilemma(欧拉通路)

    In a country, there are a number of cities. Each pair of city is connected by a highway, bi-directio ...

  4. 用友NC客户端地址

    http://uclient.yonyou.com/liyan5(李艳) 08-23 14:29:41在这里输入http://10.0.0.67:80

  5. 【bzoj2527】[Poi2011]Meteors(树状数组(单点查询,区间修改)+整体二分)

    [bzoj2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...

  6. 斯特林(Stirling)公式 求大数阶乘的位数

    我们知道整数n的位数的计算方法为:log10(n)+1n!=10^m故n!的位数为 m = log10(n!)+1 lgN!=lg1+lg2+lg3+lg4+lg5+................. ...

  7. Strongly connected-HDU4635

    Problem - 4635 http://acm.hdu.edu.cn/showproblem.php?pid=4635 题目大意: n个点,m条边,求最多再加几条边,然后这个图不是强连通 分析: ...

  8. codevs——1031 质数环

    1031 质数环  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 一个大小为N(N<=17 ...

  9. 洛谷 P2237 [USACO14FEB]自动完成Auto-complete

    P2237 [USACO14FEB]自动完成Auto-complete 题目描述 Bessie the cow has a new cell phone and enjoys sending text ...

  10. mkswap,swapon, swapoff命令:创建交换分区

    linux支持虚拟内存,用作虚拟内存的硬盘部分被称为交互空间(swap space),虚拟内存是指使用磁盘当作内存的扩展,这样可用内存的大小就相应的增大了.内核会将暂时不用的内存块的内容写到硬盘上,从 ...