【BZOJ2959】长跑(Link-Cut Tree,并查集)
【BZOJ2959】长跑(Link-Cut Tree,并查集)
题面
题解
如果保证不出现环的话
妥妥的\(LCT\)傻逼题
现在可能会出现环
环有什么影响?
那就可以沿着环把所有点全部走一遍吧
所以,相当于把环看成一个点来搞一搞
所以,维护一个并查集
记录一下每个点被缩成了哪个点
然后再用\(LCT\)维护缩点后的树就行啦
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 320000
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Node
{
int ch[2],ff;
int sum,rev,v;
}t[MAX<<1];
int S[MAX<<1],top;
int tot,n,m,a[MAX<<1];
int f[MAX<<1];
int ff[MAX<<1];
int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}
int Getf(int x){return x==ff[x]?x:ff[x]=Getf(ff[x]);}
bool isroot(int x){return t[getf(t[x].ff)].ch[0]!=x&&t[getf(t[x].ff)].ch[1]!=x;}
void pushup(int x){t[x].sum=t[lson].sum+t[rson].sum+t[x].v;}
void pushdown(int x)
{
if(!t[x].rev)return;
swap(lson,rson);
t[lson].rev^=1;t[rson].rev^=1;
t[x].rev^=1;
}
void rotate(int x)
{
int y=getf(t[x].ff),z=getf(t[y].ff);
int k=t[y].ch[1]==x;
if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
t[x].ch[k^1]=y;t[y].ff=x;
pushup(y);pushup(x);
}
void Splay(int x)
{
S[top=1]=x=getf(x);
for(int i=x;!isroot(i);i=getf(t[i].ff))S[++top]=getf(t[i].ff);
while(top)pushdown(S[top--]);
while(!isroot(x))
{
int y=getf(t[x].ff),z=getf(t[y].ff);
if(!isroot(y))
(t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x){x=getf(x);for(int y=0;x;y=x,x=getf(t[x].ff))Splay(x),t[x].ch[1]=y,pushup(x);}
void makeroot(int x){x=getf(x);access(x);Splay(x);t[x].rev^=1;}
void split(int x,int y){x=getf(x);y=getf(y);makeroot(x);access(y);Splay(y);}
void link(int x,int y){x=getf(x);y=getf(y);makeroot(x);t[x].ff=y;pushup(y);}
int findroot(int x){getf(x);access(x);Splay(x);while(lson)x=lson;return x;}
void dfs(int x)
{
f[getf(x)]=tot;
t[tot].v+=t[x].v;
t[tot].sum+=t[x].v;
if(lson)dfs(lson);
if(rson)dfs(rson);
}
void Link(int x,int y)
{
if(Getf(x)!=Getf(y))ff[Getf(x)]=Getf(y),link(getf(x),getf(y));
else
{
x=getf(x);y=getf(y);
++tot;ff[tot]=f[tot]=tot;
split(x,y);
dfs(y);
}
}
void Modify(int u,int v)
{
makeroot(getf(u));
t[getf(u)].v-=a[u];a[u]=v;
t[getf(u)].v+=a[u];
pushup(getf(u));
}
int Query(int u,int v)
{
if(Getf(u)!=Getf(v))return -1;
u=getf(u);v=getf(v);
split(u,v);
return t[v].sum;
}
int main()
{
tot=n=read();m=read();
for(int i=1;i<=n;++i)f[i]=ff[i]=i,t[i].v=a[i]=read();
int opt,u,v;
while(m--)
{
opt=read();u=read();v=read();
if(opt==1)Link(u,v);
else if(opt==2)Modify(u,v);
else printf("%d\n",Query(u,v));
}
return 0;
}
【BZOJ2959】长跑(Link-Cut Tree,并查集)的更多相关文章
- Link Cut Tree 总结
Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- (RE) luogu P3690 【模板】Link Cut Tree
二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...
- 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 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
随机推荐
- 【转】Nginx配置详解
转自:http://www.cnblogs.com/knowledgesea/p/5175711.html Nginx常用功能 1. Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反 ...
- 用mount挂载远程服务器网络硬盘
环境: 服务器:192.168.20.204 客户端:192.168.20.203 1. 在服务器配置/etc/export 添加可以共享的文件夹和允许的客户端地址 /home/dir 192.16 ...
- 我的docker全套流程例子
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 下文是自己从搭建docker到docker里安装mysql到 ...
- 使用mybatis插入自增主键ID的数据后返回自增的ID
在开发中碰到用户注册的功能需要用到用户ID,但是用户ID是数据库自增生成的,这种情况上网查询后使用下面的方式配置mybatis的insert语句可以解决: <insert id="in ...
- linux 命令:tr 的简单使用
工作的需要,用到了tr命令,因为用到的次数不是很多,怕以后忘记了百度,就自己总结下.例子什么的,copy linux shell 脚本攻略这本书. tr:常用选项 -c 用字符串1中字符集的补集替换此 ...
- 微信小程序(一)
开发流程 注册微信小程序并申请微信支付-->制作小程序-->上传并提交审核-->审核通过,小程序上线 开发微信小程序需要准备 企业公众号(被认证)以及申请小程序.微信开发技术.S ...
- CodeForces - 730A 贪心+模拟
贪心策略: 1.只有一个最大值,选着第二大的一起参加比赛减分. 2.有奇数个最大值,选择三个进行比赛. 3.偶数个最大值,选择两个进行比赛. 为什么不把最大值全部选择? 因为最多只能选五个,有可能选择 ...
- javascript 面向对象(多种创建对象的方式)
创建对象 第一种:基于Object对象 var person = new Object(); person.name = 'My Name'; person.age = 18; person.getN ...
- Nginx 调优经验记录
1.2017年连续爆出5.x版本xshell安全问题和intel的cpu设计漏洞 ,此时我就注意到尽量少暴露自己线上使用的工具以及版本.例如:mysql版本,以及缓存层策略,服务器版本等,以下为 隐藏 ...
- 【其他】Objective-C 内存管理学习总结
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/OC_CG.html 最近学习了Objective-C语言(以下简称OC),其他的都还好,唯有它的内存管理让我不知所 ...