bzoj4998 星球联盟 LCT + 并查集

题目传送门

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

题解

根据题意,就是要动态维护点双,求出一个点双的权值和。

所以这道题就是和 bzoj2959 长跑 一样了,每一次枚举一条路径上的点双暴力和合并,并查集维护。

本来是想再练一练 LCT 码力的,结果又调了半天。

大概错误都有:

  1. connect 函数的 \(fa\) 和 \(o\) 的顺序写错;
  2. 每一次把一条链合并成一个点双的时候需要把代表点的孩子删掉!!!
  3. 要把连边和 dfs 一起写,因为 dfs 的时候初始点双还没有分清楚。


时间复杂度 \(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 = 200000 + 7;

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

int n, m, Q, tp, dfc, bccno;
int a[N], st[N], stt[N], dfn[N], low[N], bcc[N];
int fa[N];

struct Edge { int to, ne; } g[N << 1]; int head[N], tot = 1;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }

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

struct Node { int c[2], fa, rev, v, s, sum; } t[N];
inline bool isroot(int o) { return t[find(t[o].fa)].lc != o && t[find(t[o].fa)].rc != o; }
inline bool idtfy(int o) { return t[find(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) {
    assert(find(o) == o);
    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 = 0;
}
inline void rotate(int o) {
    assert(find(o) == o);
    assert(!isroot(o));
    int fa = find(t[o].fa), pa = find(t[fa].fa), d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
//  dbg("o = %d, fa = %d, pa = %d, d1 = %d, d2 = %d, b = %d\n", o, fa, pa, d1, d2, b);
    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(!t[0].lc && !t[0].rc);
    assert(t[o].fa != o), assert(t[b].fa != b), assert(t[fa].fa != fa), assert(t[pa].fa != pa);
}
inline void splay(int o) {
//  dbg("splay: o = %d, t[o].fa = %d\n", o, t[o].fa);
    int x = o, tp = 0;
    stt[++tp] = x;
    while (!isroot(x)) /*dbg("splay_stakc: o = %d, x = %d, t[x].fa = %d, find(t[x].fa) = %d\n", o, x, t[x].fa, find(t[x].fa)), */stt[++tp] = x = find(t[x].fa);
    while (tp) pushdown(stt[tp--]);
    while (!isroot(o)) {
        int fa = find(t[o].fa);
//      dbg(": o = %d, fa = %d, isroot(o) = %d, isroot(fa) = %d\n", o, fa, (int)isroot(o), (int)isroot(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) {
//  dbg("**** begin access, o = %d\n", o);
    for (int x = 0; o; o = find(t[x = o].fa))
        splay(o), t[o].rc = x, pushup(o);
//  dbg("**** end access\n");
}
inline void mkrt(int o) {
    access(o), splay(o);
    t[o].rev ^= 1, std::swap(t[o].lc, t[o].rc);
}
inline int getrt(int o) {
    access(o), splay(o);
    while (pushdown(o), t[o].lc) o = t[o].lc;
    return splay(o), o;
}
inline void link(int x, int y) {
//  dbg("x = %d, y = %d\n", x, y);
    mkrt(x);
    if (getrt(y) != x) t[x].fa = y;
}

inline void dfs1(int x, int fr = 0) {
//  dbg("x = %d, fr = %d\n", x, fr);
    dfn[x] = low[x] = ++dfc, st[++tp] = x;
    for fec(i, x, y) if ((i ^ fr) != 1) {
        if (!dfn[y]) dfs1(y, i), smin(low[x], low[y]);
        else if (!bcc[y]) smin(low[x], dfn[y]);
    }
    if (dfn[x] == low[x]) {
        ++bccno;
        while (1) {
            int y = st[tp--];
            bcc[y] = bccno, fa[y] = x, x != y && (t[x].v += t[y].v);
//          dbg("is bcc : x = %d, y = %d, tp = %d, st* = %d\n", x, y, tp, st[1]);;
            if (x == y) break;
        }
        pushup(x);
    }
}

inline void dfs2(int o) {
    if (!o) return;
    assert(find(o) == o);
    pushdown(o);
    dfs2(t[o].lc);
    st[++tp] = o;
    dfs2(t[o].rc);
}

inline void work() {
    for (int i = 1; i <= n; ++i) if (!dfn[i]) dfs1(i);
    for (int x = 1; x <= n; ++x) for fec(i, x, y) link(find(x), find(y));
//  dbg("*****\n");
    while (Q--) {
        int x, y;
        read(x), read(y);
//      dbg("x = %d, y = %d\n", x, y);
        x = find(x), y = find(y);
        if (getrt(x) != getrt(y)) link(x, y), puts("No");
        else if (x == y) printf("%d\n", t[x].v);
        else {
//          dbg("query : x = %d, y = %d\n", x, y);
            mkrt(x), access(y), splay(y);
            tp = 0, dfs2(y);
            for (int i = 1; i < tp; ++i) t[st[tp]].v += t[st[i]].v, fa[st[i]] = st[tp];
            t[st[tp]].lc = 0, pushup(st[tp]), printf("%d\n", t[st[tp]].v);
//          dbg("************* x = %d, y = %d, st[1] = %d, %d\n", x, y, st[tp], t[st[tp]].fa);
        }
    }
}

inline void init() {
    read(n), read(m), read(Q);
    int x, y;
    for (int i = 1; i <= m; ++i) read(x), read(y), adde(x, y);
    for (int i = 1; i <= n; ++i) fa[i] = i, t[i].v = 1, 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/bzoj4998.html

时间: 2024-07-29 03:15:56

bzoj4998 星球联盟 LCT + 并查集的相关文章

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

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

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

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

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

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

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

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

题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2959 题解 调了半天,终于调完了. 显然题目要求是求出目前从 \(A\) 到 \(B\) 的可以经过重复的点(权值只算一次)的最长路. 考虑把无向图边双缩点以后,形成一棵树的关系.可以发现,边双内的点的点权是可以都取到的.所以边双缩点以后直接求出树上两个点之间的点权之和就可以了. 但是考虑怎么维护这个边双. 新链接一条边的时候,如果两个端点不连通,那么直接连上就可以了. 如果两个端点联通,那

2959: 长跑|LCT+并查集

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

小结:并查集

复杂度: O(n*α(n)) 其中α(x),对于x=宇宙中原子数之和,α(x)不大于4 .(对于nocow里的复杂度我也是醉了) 概要: 并查集就是一个数组和一行话. 应用: 图的连通.集合操作.生成树的合并等 技巧及注意: 并查集是个好东西. 维护区间+前缀和:对于一些连续的区间,我们要判断这些区间是否合法,带修改.这种题我们可以考虑并查集来维护区间,用前缀和维护信息,而在并查集按秩合并的时候,一定要注意合并前和合并后如何维护信息,比如这题[BZOJ]1202: [HNOI2005]狡猾的商人

bzoj 2959 长跑(LCT+BCC+并查集)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2959 [题意] n个点,提供操作:连边,修改点权,查询自定义边的方向后起点a终点b能经过的最大点权和. [思路] 对于一个边的双连通分量,显然可以将权值全部获得. 如果没有连边操作,我们只需要将一个bcc缩点后求得a->b路径上的点权和即可. 加上连边后,使用并查集代表一个bcc,如果u,v之间不连通直接连边,如果已经连通则构成一个bcc,使用并查集将LCT的所有节点合并. 注意缩点

BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集

题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了...... 分析: 就是用lct维护最大生成树. 然后如果去UOJ上面交发现如果不用并查集判断连通性就要T?! 然后我就默默改了并查集...(hash表并查集输入输出占据了一半的行数?!) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdl