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

题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=2959

题解

调了半天,终于调完了。



显然题目要求是求出目前从 \(A\) 到 \(B\) 的可以经过重复的点(权值只算一次)的最长路。

考虑把无向图边双缩点以后,形成一棵树的关系。可以发现,边双内的点的点权是可以都取到的。所以边双缩点以后直接求出树上两个点之间的点权之和就可以了。

但是考虑怎么维护这个边双。

新链接一条边的时候,如果两个端点不连通,那么直接连上就可以了。

如果两个端点联通,那么路径上的所有点都会进入同一个边双。可以发现每个点只会进入一次边双,所以可以暴力取出这些点加入边双。

用 LCT 维护就可以了。



说起来可真轻松呢。

但是写起来的细节好多啊:

  1. 对于求出一个点属于哪个边双,可以用一个并查集来维护。这样的话,LCT 上每个点的父亲啊,孩子啊都会受到影响,所以只要用了父亲孩子之类的东西都要在并查集里面跑一遍。一定要注意有没有哪里没有跑并查集。
  2. 判断连通性的时候用 LCT 自带的找根操作太慢了,请用并查集。
  3. 这样下来有了两个并查集呢,不要搞混了。
  4. pushdown!!! pushdown 一定不能漏!!


时间复杂度只有 LCT,所以是 \(o(m\log n)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 1.5e5 + 7;

#define lc c[0]
#define rc c[1]

int n, m;
int a[N], fa[N], fa2[N], lian[N];

inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline int find2(int x) { return fa2[x] == x ? x : fa2[x] = find2(fa2[x]); }

inline void self_kill() { std::cerr << a[N * 100]; }

struct Node { int c[2], fa, rev, s, v, sum; } t[N];
int st[N];
inline bool isroot(int o) { return find2(t[find2(t[o].fa)].lc) != o && find2(t[find2(t[o].fa)].rc) != o; }
inline bool idtfy(int o) { return find2(t[find2(t[o].fa)].rc) == o; }
inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
inline void pushup(int o) {
    if (find2(o) != o) dbg("o = %d, find2(o) = %d\n", o, find2(o));
    if (find2(o) != o) self_kill();
    t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
    t[o].sum = t[t[o].lc].sum + t[t[o].rc].sum + t[o].v;
}
inline void pushdown(int o) {
    if (!t[o].rev) return;
    if (t[o].lc) t[t[o].lc].rev ^= 1, std::swap(t[t[o].lc].lc, t[t[o].lc].rc);
    if (t[o].rc) t[t[o].rc].rev ^= 1, std::swap(t[t[o].rc].lc, t[t[o].rc].rc);
    t[o].rev ^= 1;
}
inline void rotate(int o) {
    int fa = find2(t[o].fa), pa = find2(t[fa].fa), d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
    if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
    connect(o, fa, d1 ^ 1), connect(fa, b, d1);
    pushup(fa), pushup(o);
    assert(o != t[o].fa);
}
inline void splay(int o) {
    if (find2(o) != o) self_kill();
    assert(find2(o) == o);
    int x = o, tp = 0;
    st[++tp] = x;
    while (!isroot(x)) st[++tp] = x = find2(t[x].fa);
    while (tp) pushdown(st[tp--]);
    while (!isroot(o)) {
        int fa = find2(t[o].fa);
        assert(o != fa);
        if (isroot(fa)) rotate(o);
        else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
        else rotate(o), rotate(o);
    }
}
inline void access(int o) {
    assert(find2(o) == o);
    for (int x = 0; o; o = find2(t[x = o].fa))
        splay(o), t[o].rc = x, pushup(o);
}
inline void mkrt(int o) {
    assert(find2(o) == o);
    access(o), splay(o);
    t[o].rev ^= 1, std::swap(t[o].lc, t[o].rc);
}
inline int getrt(int o) {
    assert(find2(o) == o);
    access(o), splay(o);
    while (pushdown(o), t[o].lc) o = find2(t[o].lc);
    return splay(o), o;
}
inline void link(int x, int y) {
    assert(find2(x) == x);
    assert(find2(y) == y);
    mkrt(x);
    t[x].fa = y;
}
inline void cut(int x, int y) {
    assert(find2(x) == x);
    assert(find2(y) == y);
    mkrt(x), access(y), splay(y);
    if (t[y].lc == x && t[x].rc == 0) t[x].fa = t[y].lc = 0, pushup(y);
}

inline void dfs(int o) {
    if (!o) return;
    pushdown(o);
    dfs(find2(t[o].lc));
    lian[++lian[0]] = o;
    dfs(find2(t[o].rc));
}

inline void work() {
    while (m--) {
        int opt, x, y;
        read(opt), read(x), read(y);
        if (opt == 1) {
            x = find2(x), y = find2(y);
            if (find(x) != find(y)) link(x, y), fa[find(x)] = find(y);
            else {
                mkrt(x), access(y), splay(y);
                lian[0] = 0, dfs(y);
                for (int i = 1; i < lian[0]; ++i) fa2[lian[i]] = y, t[y].v += t[lian[i]].v;
                assert(find2(y) == y);
                pushup(y);
            }
        } else if (opt == 2) splay(find2(x)), t[find2(x)].v += y - a[x], a[x] = y, pushup(find2(x));
        else {
            x = find2(x), y = find2(y);
            if (find(x) != find(y)) puts("-1");
            else mkrt(x), access(y), splay(y), printf("%d\n", t[y].sum);
        }
    }
    puts("");
}

inline void init() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) read(a[i]);
    for (int i = 1; i <= n; ++i) t[i].v = a[i], fa[i] = fa2[i] = i, pushup(i);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

原文地址:https://www.cnblogs.com/hankeke/p/bzoj2959.html

时间: 2024-08-01 20:56:22

bzoj2959: 长跑 LCT+并查集+边双联通的相关文章

bzoj2959: 长跑(LCT+并查集)

题解 动态树Link-cut tree(LCT)总结 LCT常数大得真实 没有环,就是\(lct\)裸题吧 有环,我们就可以绕环转一圈,缩点 怎么搞? 当形成环时,把所有点的值全部加到一个点上,用并查集维护加到哪个点上 判断连通性再用一个并查集 Code #include<bits/stdc++.h> #define LL long long #define RG register using namespace std; template<class T> inline void

2959: 长跑|LCT+并查集

慎入-此人代码自带5倍常数.. 静态的话就是随便搞出一棵生成树来,然后把环缩起来,询问的答案就是路径上的权值和 动态的就需要LCT来维护生成树,每遇到连起边来就形成环的情况时,就把这个环缩成一个点 动态的查询一条链上的权值和. 为什么我的代码的常数这么大--.后几个点在本地跑5s #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cst

poj 2513 欧拉回路+并查集判断是否联通+Trie树

http://poj.org/problem?id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得以前看过,trie代替map,尤其当数据量特别大的时候 学到了: 1.Trie代替map的思想,可以在单词结尾的tree[i][tk]  这个i作为字符串对应的int值 ,当然这个int值也可以用于建立并查集 2.接上,通过并查集判断,所有的点在同一个集合图就是联通的,否则不联通,注意tree[i][tk]>0 表示是单词结尾, x=Find(x);//这句

bzoj4998 星球联盟 LCT + 并查集

题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4998 题解 根据题意,就是要动态维护点双,求出一个点双的权值和. 所以这道题就是和 bzoj2959 长跑 一样了,每一次枚举一条路径上的点双暴力和合并,并查集维护. 本来是想再练一练 LCT 码力的,结果又调了半天. 大概错误都有: connect 函数的 \(fa\) 和 \(o\) 的顺序写错: 每一次把一条链合并成一个点双的时候需要把代表点的孩子删掉!!! 要把连边和 dfs 一起写

【BZOJ4998】星球联盟 LCT+并查集

[BZOJ4998]星球联盟 Description 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个星球,使得它们能够相互到达.若两个星球属于同一个联盟,则必须存在一条环形线路经过这两个星球,即两个星球间存在两条没有公共隧道的路径.为了壮大联盟的队伍,这些星球将建设P条新的太空隧道.这P条新隧道将按顺序依次建成.一条新轨道建成后,可能会使一些星球属于同一

BZOJ 2959 长跑 Link-Cut-Tree+并查集

题目大意:给定n个点,支持以下操作: 1.在某两个点之间连接一条无向边 2.改变某个点的权值 3.将每条边设定一个方向,然后从x走到y,求能经过的所有点的权值和 首先如果这个图是静态的,我们把边双都缩点,那么每次询问显然就是两个点所在边双路径上的点权和 现在图是动态的,因此我们用动态树维护一下就行了 如果连边的两个点不连通,就在LCT中连接这两个点 如果连边的两个点已经连通,就将这个两个点路径上的所有点用并查集缩点 时间复杂度O(mlogn) 似乎用链剖会快一些? #include <cstdi

【bzoj4998】星球联盟(并查集+边双)

题面 传送门 题解 总算有自己的\(bzoj\)账号啦! 话说这题好像\(Scape\)去年暑假就讲过--然而我到现在才会-- \(LCT\)什么的跑得太慢了而且我也不会,所以这里是一个并查集的做法 首先题目意思就是要我们动态维护点双 我们离线,先求出一个森林,并且要使用编号尽量小的边 连上一条边的时候,如果它们还没有联通,那么显然答案是\(No\) 如果已经联通,那么它们这棵树的路径上所有点都会被缩进同一个点双里.暴力的话复杂度显然爆炸 我们另外开一个并查集\(ga\),表示\(i\)所在的边

【BZOJ2594】水管局长加强版,LCT+并查集+二分查找位置

Time:2016.05.10 Author:xiaoyimi 转载注明出处谢谢 传送门 思路: LCT维护路径最小值 倒叙处理询问,就相当于往图里面加边. 实时维护最小值,即最小生成树,可以参照魔法森林. 最初的最小生成树操作用kruskal 最蛋疼的是处理询问时你不知道要删除哪条边,这给kruskal带来很大麻烦,所以我们对原来的每一条边使其编号小的端点在前,大的在后,然后以左端点为第一关键字,右端点为第二关键字排序,记录下每个左端点的所在区间,然后就可以通过二分查找的方式来确定是哪条边了

poj 2513 欧拉回路+并查集推断是否联通+Trie树

http://poj.org/problem? id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学到了: 1.Trie取代map的思想,能够在单词结尾的tree[i][tk]  这个i作为字符串相应的int值 .当然这个int值也能够用于建立并查集 2.接上.通过并查集推断.全部的点在同一个集合图就是联通的,否则不联通,注意tree[i][tk]>0 表示是单词结尾. x=Find(x);//这