【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 ...
随机推荐
- Java常用日志框架介绍
Java常用日志框架介绍 java日志概述 对于一个应用程序来说日志记录是必不可少的一部分.线上问题追踪,基于日志的业务逻辑统计分析等都离不日志.java领域存在多种日志框架,目前常用的日志框架包括L ...
- Maven中避开测试环节
两种方法 修改pom文件 添加<skipTests>true</skipTests>标签 <plugin> <groupId>org.apache.ma ...
- Gitbucket—快速建立自己的Github
GitBucket是一个用Scala语言编写的类似Github的应用,界面非常相似.它非常容易安装–容易到你只需要把它的war文件扔到tomcat中,然后启动tomcat就直接可以访问了.或者直接ja ...
- ubuntu终端常用命令及solarized配色(护眼)
ubuntu终端常用命令及solarized配色(护眼) ubuntu 终端 命令 1.常用命令 ctrl + l - 清屏 . cLear ctrl + c - 终止命令. ctrl + d ...
- uva10603 倒水问题
状态搜索.类似八数码问题 AC代码 #include<cstdio> #include<queue> #include<cstring> #include<a ...
- 【BZOJ3993】 星际战争
Time Limit: 1000 ms Memory Limit: 128 MB Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一 ...
- Spring cloud oauth2.0 access_token 永不失效设置方法
在AuthorizationServerConfigurerAdapter,重写一个TokenServices,注意这里的@Primary 非常重要,否则会有3个同类型的Bean,无法注入,会抛出以下 ...
- AES-256加密解密
/// <summary> /// AES-256加密 /// </summary> /// <param name="toEncrypt">& ...
- 【重磅】PRO基础版免费,是时候和ExtJS说再见了!
三石的新年礼物 9 年了,FineUI(开源版)终于迎来了她的继任者 - FineUIPro(基础版),并且完全免费! FineUIPro(基础版)作为三石奉献给社区的一个礼物,绝对让你心动: 拥 ...
- linux 运维 nginx服务器
nginx(web服务器) nginx是一个高性能的http和反向代理服务器,同时也是一个imap/pop3/smtp 代理服务器比apache简单官网:http://nginx.org nginx配 ...