BZOJ 2959 长跑 (LCT+并查集)
题面:BZOJ传送门
当成有向边做的发现过不去样例,改成无向边就忘了原来的思路..
因为成环的点一定都能取到,我们把它们压成一个新点,权值为环上所有点的权值和
这样保证了图是一颗森林
每次询问转化为,取出$a$到$b$这条链,求链上所有点的权值和
这实际是一个不删边的动态维护边双的过程
可以用$LCT$维护
加入一条边$<x,y>$时,我们取出链$x,y$
如果$x,y$原来不连通,把它们连上
否则说明$x,y$原来就联通的,连上这条边会成环,把$x,y$这条链上的点全都压成一个点,用并查集维护
每次$access$都在并查集里找父亲就行了
维护权值的时候细节比较多
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N1 150010
#define M1 (N1<<1)
#define il inline
#define idx(X) (X-'a')
using namespace std; char str[M1];
int n,m,de;
int a[N1],r[N1]; struct Union{
int fa[N1];
int findfa(int x)
{
int pre,y=x;
if(!x) return ;
while(fa[y]!=y) y=fa[y];
while(fa[x]!=y){ pre=fa[x]; fa[x]=y; x=pre; }
return y;
}
}U;
struct LCT{
int ch[N1][],fa[N1],rev[N1],sum[N1],stk[N1],tp;
il int idf(int x){ return (ch[fa[x]][]==x)?:;}
il void pushup(int x){ sum[x]=sum[ch[x][]]+sum[ch[x][]]+a[x]; }
il int isroot(int x){ return (ch[fa[x]][]!=x&&ch[fa[x]][]!=x)?:; }
il void revers(int x){ swap(ch[x][],ch[x][]),rev[x]^=; }
il void pushdown(int x)
{
if(rev[x])
{
if(ch[x][]) revers(ch[x][]);
if(ch[x][]) revers(ch[x][]);
rev[x]^=;
}
}
il void rot(int x)
{
int y=fa[x],ff=fa[y],px=idf(x),py=idf(y);
if(!isroot(y)) ch[ff][py]=x; fa[x]=ff;
fa[ch[x][px^]]=y; ch[y][px]=ch[x][px^];
fa[y]=x; ch[x][px^]=y;
pushup(y),pushup(x);
}
void splay(int x)
{
int y=x; stk[++tp]=x;
while(!isroot(y)){stk[++tp]=fa[y],y=fa[y];}
while(tp){pushdown(stk[tp--]);}
while(!isroot(x))
{
y=fa[x];
if(isroot(y)) rot(x);
else if(idf(y)==idf(x)) rot(y),rot(x);
else rot(x),rot(x);
}
}
void access(int x)
{
for(int y=;x;y=x,fa[x]=U.findfa(fa[x]),x=fa[x])
splay(x),ch[x][]=y,pushup(x);
}
void mkroot(int x)
{
access(x);
splay(x);
revers(x);
}
int findroot(int x)
{
access(x); splay(x);
while(ch[x][]) pushdown(ch[x][]),x=ch[x][];
splay(x); return x;
}
void split(int x,int y)
{
mkroot(x);
access(y);
splay(y);
}
void dfs(int x,int root)
{
U.fa[x]=root;
if(ch[x][]) dfs(ch[x][],root);
if(ch[x][]) dfs(ch[x][],root);
}
int link(int x,int y)
{
/*if(x==4&&y==1)
de=1; */
mkroot(x); access(y); splay(y);
int f=findroot(y);
if(f==x){ //merge
dfs(f,f); a[f]=sum[f];
ch[f][]=ch[f][]=;
}else{ //link
fa[x]=y;
}
}
int query(int x,int y)
{
split(x,y);
if(findroot(y)!=x) return -;
else return sum[x];
}
}lct; int main()
{
scanf("%d%d",&n,&m);
int i,j,A,B,x,y,v,fl,ans=,id=,de;
for(i=;i<=n;i++)
scanf("%d",&a[i]), lct.sum[i]=a[i], r[i]=a[i], U.fa[i]=i;
while(m--)
{
scanf("%d%d%d",&fl,&A,&B);
if(fl==)
{
x=U.findfa(A); y=U.findfa(B);
lct.link(x,y);
}
if(fl==)
{
x=U.findfa(A); lct.splay(x);
lct.sum[x]+=B-r[A];
a[x]+=B-r[A];
r[A]=B;
}
if(fl==)
{
x=U.findfa(A); y=U.findfa(B);
printf("%d\n",lct.query(x,y));
}
}
return ;
}
BZOJ 2959 长跑 (LCT+并查集)的更多相关文章
- BZOJ 2959: 长跑 LCT_并查集_点双
真tm恶心...... Code: #include<bits/stdc++.h> #define maxn 1000000 using namespace std; void setIO ...
- 【bzoj2959】长跑 LCT+并查集
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...
- BZOJ 2959 长跑 (LCT、并查集)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2959 题解 真是被这题搞得心态大崩--调了7个小时--然而并查集都能写成\(O(n^2) ...
- BZOJ 2959: 长跑 lct 双联通分量 并查集 splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2959 用两个并查集维护双联通分量的编号和合并. #include<iostream> # ...
- BZOJ 2959: 长跑 [lct 双连通分量 并查集]
2959: 长跑 题意:字词加入边,修改点权,询问两点间走一条路径的最大点权和.不一定是树 不是树
- BZOJ2959长跑——LCT+并查集(LCT动态维护边双连通分量)
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前. 为 ...
- bzoj2959: 长跑 LCT+并查集+边双联通
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2959 题解 调了半天,终于调完了. 显然题目要求是求出目前从 \(A\) 到 \(B\) 的可 ...
- bzoj2959: 长跑(LCT+并查集)
题解 动态树Link-cut tree(LCT)总结 LCT常数大得真实 没有环,就是\(lct\)裸题吧 有环,我们就可以绕环转一圈,缩点 怎么搞? 当形成环时,把所有点的值全部加到一个点上,用并查 ...
- 【bzoj4998】星球联盟 LCT+并查集
题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...
随机推荐
- 【ACM】hdu_1094_A+BVI_201307261731
A+B for Input-Output Practice (VI)Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
- C语言遍历文件和文件夹——————【Badboy】
[cpp] #include #include #include #include #include #include #include #define MAX_PATH_LENGTH 512 #de ...
- Delicious Apples (hdu 5303 贪心+枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- JS禁用右键+禁用Ctrl+u+禁用F12
第一种方法: , , ]; document.oncontextmenu = new Function("event.returnValue=false;"),//禁用右键 doc ...
- Qt由pcm数据生成wav文件
void AudioGrabber::saveWave(const QString &fileName, const QByteArray &raw, const QAudioForm ...
- JFinal Starting scanner at interval of 5 seconds.报错
Starting JFinal 2.0 Starting scanner at interval of 5 seconds. Starting web server on port: 80 Excep ...
- Referenced file contains errors (http://www.springframework.org/schema/beans/spring-beans-3.0.xsd)
问题: java项目在Eclipse中xml有小红叉 Problems:Referenced file contains errors (http://www.springframework.org/ ...
- DNS Tunneling及相关实现——总之,你发起攻击都需要一个DNS server,下载一些工具作为client发起数据,server收集数据并响应
摘自:http://www.freebuf.com/sectool/112076.html DNS Tunneling,是隐蔽信道的一种,通过将其他协议封装在DNS协议中传输建立通信.因为在我们的网络 ...
- Path Sum II 总结DFS
https://oj.leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf p ...
- 基于RHEL8/CentOS8的网络IP配置详解
➡ 在rhel8(含centos8)上,没有传统的network.service,在/etc/sysconfig/network-scripts/里也看不到任何脚本文件,那么该如何进行网络配置呢. ➡ ...