lct+并查集

联赛之后忘了很多东西 复习一下

这并不是一棵树,所以我们不能直接上lct

但是把双联通分量缩了以后就是一棵树了

怎么缩呢 就是把splay拆了合并到一个点上

连通性和双联通分量拿两个并查集维护

access的时候x=find(fa[x])

#include<bits/stdc++.h>
using namespace std;
const int N = 1.5e5 + ;
struct ufs {
int fa[N];
ufs() { for(int i = ; i < N; ++i) fa[i] = i; }
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void link(int u, int v)
{
fa[find(u)] = find(v);
}
bool con(int u, int v)
{
return find(u) == find(v);
}
} a, b;
int n, m;
int val[N];
queue<int> q;
namespace lct
{
struct node {
int rev, sum, w, f;
int ch[];
} t[N];
bool isr(int x) { return !t[x].f || (x != t[t[x].f].ch[] && x != t[t[x].f].ch[]); }
int wh(int x) { return x == t[t[x].f].ch[]; }
void paint(int x)
{
t[x].rev ^= ;
swap(t[x].ch[], t[x].ch[]);
}
void upd(int x)
{
t[x].sum = t[t[x].ch[]].sum + t[t[x].ch[]].sum + t[x].w;
}
void pd(int x)
{
if(!t[x].rev) return;
paint(t[x].ch[]);
paint(t[x].ch[]);
t[x].rev ^= ;
}
void rotate(int x)
{
int y = t[x].f, z = t[y].f, w = wh(x);
if(!isr(y)) t[z].ch[wh(y)] = x;
t[x].f = z;
t[y].ch[w] = t[x].ch[w ^ ];
t[t[x].ch[w ^ ]].f = y;
t[x].ch[w ^ ] = y;
t[y].f = x;
upd(y);
upd(x);
}
void ptd(int x)
{
if(!isr(x)) ptd(t[x].f);
pd(x);
}
void splay(int x)
{
ptd(x);
for(; !isr(x); rotate(x))
if(!isr(t[x].f)) rotate(wh(x) == wh(t[x].f) ? t[x].f : x);
}
void access(int x)
{
for(int p = ; x; p = x, x = b.find(t[x].f))
{
splay(x);
t[x].ch[] = p;
t[p].f = x;
upd(x);
}
}
void rever(int x)
{
access(x);
splay(x);
paint(x);
}
void expose(int x, int y)
{
rever(y);
access(x);
splay(x);
}
void link(int u, int v)
{
rever(u);
t[u].f = v;
}
void rebuild(int x, int y)
{
int rt = x;
q.push(x);
expose(x, y);
while(!q.empty())
{
x = q.front();
b.link(x, rt);
q.pop();
if(t[x].ch[]) q.push(t[x].ch[]);
if(t[x].ch[]) q.push(t[x].ch[]);
t[x].ch[] = t[x].ch[] = ;
}
t[rt].w = t[rt].sum;
}
}
void link(int u, int v)
{
u = b.find(u);
v = b.find(v);
if(u == v) return;
if(!a.con(u, v)) lct::link(u, v), a.link(u, v);
else lct::rebuild(u, v);
}
int query(int u, int v)
{
u = b.find(u);
v = b.find(v);
if(!a.con(u, v)) return -;
if(u == v) return lct::t[u].w;
lct::expose(u, v);
return lct::t[u].sum;
}
void add(int x, int v)
{
x = b.find(x);
lct::t[x].w += v;
lct::t[x].sum += v;
lct::splay(x);
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) scanf("%d", &lct::t[i].w), val[i] = lct::t[i].sum = lct::t[i].w;
while(m--)
{
int opt, u, v;
scanf("%d%d%d", &opt, &u, &v);
if(opt == ) link(u, v);
if(opt == ) add(u, v - val[u]), val[u] = v;
if(opt == ) printf("%d\n", query(u, v));
}
return ;
}

bzoj2959的更多相关文章

  1. [BZOJ2959]长跑——新技能:LCT+缩圈

    [BZOJ2959]长跑 试题描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘 ...

  2. 【BZOJ2959】长跑(Link-Cut Tree,并查集)

    [BZOJ2959]长跑(Link-Cut Tree,并查集) 题面 BZOJ 题解 如果保证不出现环的话 妥妥的\(LCT\)傻逼题 现在可能会出现环 环有什么影响? 那就可以沿着环把所有点全部走一 ...

  3. bzoj2959: 长跑

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. 【BZOJ2959】长跑 (LCT+并查集)

    Time Limit: 1000 ms   Memory Limit: 256 MB Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室 ...

  5. BZOJ2959长跑——LCT+并查集(LCT动态维护边双连通分量)

    题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前. 为 ...

  6. bzoj2959: 长跑(LCT+并查集)

    题解 动态树Link-cut tree(LCT)总结 LCT常数大得真实 没有环,就是\(lct\)裸题吧 有环,我们就可以绕环转一圈,缩点 怎么搞? 当形成环时,把所有点的值全部加到一个点上,用并查 ...

  7. 【bzoj2959】长跑 LCT+并查集

    题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...

  8. bzoj2959: 长跑 LCT+并查集+边双联通

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2959 题解 调了半天,终于调完了. 显然题目要求是求出目前从 \(A\) 到 \(B\) 的可 ...

  9. BZOJ3069: [Pa2011]Hard Choice 艰难的选择

    Description Byteasar是一个很纠结的人.每次他经过Bytetown的时候都知道有至少2条不同的路径可以选择,这导致他必须花很长时间来决定走哪条路.Byteasar最近听说了Bytet ...

随机推荐

  1. 在pypy环境中运行odoo8

    PyPy是一个独立的解析器, 通过即时编译(JIT,Just-in-time)代码避免逐行解释执行来提升运行速度的(将编译过的行代码缓存起来,从而加快速度).我们一般使用的Python一般是使用C实现 ...

  2. valid-palindrome——判断带符号数字字母的字符串是否为回文

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. ftp的实现

    ftp.h #define BUFSIZE 512#define CMDSIZE 64#define ARGSIZE 64#define PASSIVE_ON 0x1 struct ftpcmd{ c ...

  4. mysql binlog配置详解

    关闭binlog,注释掉mysql配置文件中的log-bin=mysql-bin即可     baidu zone - 关闭binlog方法   cnblogs - linux下mysql配置文件my ...

  5. 简单的看Unicode和UTF-8的区别

    作者:uuspider链接:http://www.zhihu.com/question/23374078/answer/65352538来源:知乎著作权归作者所有,转载请联系作者获得授权. 举一个例子 ...

  6. centos创建本地yum仓库

    怎样发布自己软件的安装和更新YUM源 在创建之前,我们先了解些相关的内容: yum仓库可以支持三种途径提供给yum在安装的时候下载rpm包 第一种:  ftp服务  ftp:// 第二种:  http ...

  7. jvm基础(1)

    1.整型数和浮点型数的表示 原码:第一位为符号位(0为正数,1为负数). 反码:符号位不动,源码取反. 正数补码:和原码相同. 负数补码:符号位不动,反码加1. 例如5的二进制表示可以是0000010 ...

  8. 事件总线EventBus

    什么是事件总线管理? 将事件放到队列里,用于管理和分发: 保证应用的各个部分之间高效的通信及数据,事件分发: 模块间解耦: 什么是EventBus? EventBus是发布/订阅的事件总线.Event ...

  9. 怎么将linux的动态IP设置成静态IP

    例如我的eth0网卡信息如下 eth0 Link encap:Ethernet HWaddr :0C::AA:B2:CA inet addr:192.168.79.135 Bcast:192.168. ...

  10. 2015最新iherb海淘攻略-图文入门教程-6月免邮

    注:仅仅有首次下单才享有新人优惠10$,大家下单之后千万不要取消后.否则之后则不享有新人优惠. 注:眼下Sino-海淘客国际物流已取消,仅有UCS合众速递. IHerb是美国最热门的海淘海购网站之中的 ...