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. 怎样制作gif图片?怎样制作你项目的动态效果图到你的csdn?

    怎样制作gif图?怎样上传你项目的动态效果图到你的csdn? 这仅仅是笔者用的方法.有其它方法的欢迎分享. 一张或几张展示了你的项目的功能及效果的动态图放在博客文章开头会为你的文章润色不少. 相信非常 ...

  2. leetcode 题解 || Remove Nth Node From End of List 问题

    problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...

  3. Redhat hadoop2.7.2安装笔记

    本次安装是在windows7环境下安装redhat虚拟机进行的,所须要的软件例如以下: VirtualBox-5.0.16-105871-Win.exe rhel-server-5.4-x86_64- ...

  4. linux遍历目录源代码

    <pre code_snippet_id="1622396" snippet_file_name="blog_20160324_1_744516" nam ...

  5. vim 寄存器的使用

    1. 寄存器的格式 "[a~z] 2. 在复制时指定寄存器:"ayw 3. 剪切时使用寄存器:"add 3. 黏贴时指定从某个寄存器处获取数据:"ap 4. 几 ...

  6. tree related problems (update continuously)

    leetcode Binary Tree Level Order Traversal 这道题是要进行二叉树的层次遍历.对于层次遍历,最简单直观的办法就是进行BFS.于是我们仅仅须要维护一个队列就能够了 ...

  7. Mataplotlib绘图和可视化

    Mataplotlib是一个强大的python绘图和数据可视化工具包 安装方法:pip install matplotlib 引用方法:import matplotlib.pyplot as plt ...

  8. 【CUDA】CUDA开发环境搭建

    http://blog.csdn.net/tracer9/article/details/50484764 标签: CUDA并行计算NVIDIAlinux 2016-01-08 18:35 637人阅 ...

  9. Java多态特性:重载和覆写的比較

    Java重载: 在同一个类中 方法具有同样的名字,同样或不同的返回值,但參数不同的多个方法(參数个数或參数类型) public class MethoDemo{ public static void ...

  10. C#基础系列:反射笔记

    前言:使用反射也有几年了,但是一直觉得,反这个概念很抽象,今天有时间就来总结下这个知识点. 1.为什么需要反射: 最初使用反射的时候,作为小菜总是不理解,既然可以通过new 一个对象的方式得到对象,然 ...