【题解】洛谷 P5489 EntropyIncreaser 与 动态图

原题链接

题意

动态加边,维护两点 x , y x, y x,y 间割点和桥的数量。强制在线。

思路

不难想到 LCT。

动态求割点的板子题是 P5622 [DBOI2019] 巫女的职责

求割点部分放个本人的题解 链接,不多赘述了。

动态求桥的板子题是 P2542 [AHOI2005] 航线规划

显然,树上所有边都是桥;每次加边时,若出现环,则环上所有边都不是桥。

具体地,初始我们将所有边权赋值为 1 1 1,如果出现环那么将这个环上所有边权变为 0 0 0,查询操作即转化为求路径和。

于是开两棵 LCT 分别维护割点和桥即可。

代码

#include <iostream>

using namespace std;

const int N = 300010;

int n, m, la;
struct Splay_Node
{
    int s[2], p, v;
    int sum, rev, cov;
};

struct Link_Cut_Tree
{
    Splay_Node tr[N];
    int stk[N], idx, cnt;
    inline void pushrev(int x) {swap(tr[x].s[0], tr[x].s[1]), tr[x].rev ^= 1;}
    inline void pushcov(int x) {tr[x].v = tr[x].sum = 0, tr[x].cov = 1;}
    inline void pushup(int x) {tr[x].sum = tr[tr[x].s[0]].sum + tr[x].v + tr[tr[x].s[1]].sum;}
    inline bool is_root(int x) {return tr[tr[x].p].s[0] != x && tr[tr[x].p].s[1] != x;}
    inline void pushdown(int x)
    {
        if (tr[x].rev) pushrev(tr[x].s[0]), pushrev(tr[x].s[1]), tr[x].rev ^= 1;
        if (tr[x].cov) pushcov(tr[x].s[0]), pushcov(tr[x].s[1]), tr[x].cov = 0;
    }

    inline void rotate(int x)
    {
        int y = tr[x].p, z = tr[y].p;
        int k = tr[y].s[1] == x;
        if (!is_root(y)) tr[z].s[tr[z].s[1] == y] = x;
        tr[x].p = z;
        tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
        tr[x].s[k ^ 1] = y, tr[y].p = x;
        pushup(y), pushup(x);
    }

    inline void splay(int x)
    {
        int top = 0, r = x;
        stk[ ++ top] = r;
        while (!is_root(r)) stk[ ++ top] = tr[r].p, r = tr[r].p;
        while (top) pushdown(stk[top -- ]);
        while (!is_root(x))
        {
            int y = tr[x].p, z = tr[y].p;
            if (!is_root(y))
                if ((tr[y].s[1] == x) ^ (tr[z].s[1] == y)) rotate(x);
                else rotate(y);
            rotate(x);
        }
    }

    void flatten(int u)
    {
        pushdown(u);
        if (tr[u].s[0]) flatten(tr[u].s[0]);
        stk[ ++ idx] = u;
        if (tr[u].s[1]) flatten(tr[u].s[1]);
    }

    inline int access(int x)
    {
        int z = x, y;
        for (y = 0; x; y = x, x = tr[x].p)
            splay(x), tr[x].s[1] = y, pushup(x);
        splay(z);
        return y;
    }

    inline void make_root(int x) {access(x), pushrev(x);}
    inline int find_root(int x)
    {
        access(x);
        while (tr[x].s[0]) pushdown(x), x = tr[x].s[0];
        splay(x);
        return x;
    }

    inline bool judge(int x, int y) {make_root(x); return find_root(y) == x;}
    inline void split(int x, int y) {make_root(x); access(y);}
    inline bool link(int x, int y)
    {
        if (judge(x, y)) return false;
        else tr[x].p = y;
        return true;
    }
}cut, brg;

inline void add_brg(int x, int y)
{
    if (brg.judge(x, y)) brg.split(x, y), brg.pushcov(y);
    else brg.link(x, ++ brg.cnt), brg.link(y, brg.cnt);
}

inline void add_cut(int x, int y)
{
    if (cut.link(x, y)) return;
    cut.split(x, y);
    cut.idx = 0, cut.cnt ++ ;
    cut.flatten(y);
    while (cut.idx)
    {
        int u = cut.stk[cut.idx -- ];
        cut.tr[u].p = cut.cnt, cut.tr[u].s[0] = cut.tr[u].s[1] = 0;
        cut.pushup(u);
    }
}

int main()
{
    int op, x, y;
    scanf("%d%d", &n, &m), cut.cnt = brg.cnt = n;
    for (int i = 1; i <= n; i ++ ) cut.tr[i].v = 1;
    for (int i = 1; i <= m; i ++ ) brg.tr[i + n].v = 1;
        
    while (m -- )
    {
        scanf("%d%d%d", &op, &x, &y);
        x ^= la, y ^= la;
        if (op == 1) add_cut(x, y), add_brg(x, y);
        else if (op == 2)
        {
            if (!brg.judge(x, y)) puts("-1");
            else brg.split(x, y), printf("%d\n", (la = brg.tr[y].sum));
        }
        else
        {
            if (!cut.judge(x, y)) puts("-1");
            else cut.split(x, y), printf("%d\n", (la = cut.tr[y].sum));
        }
    }
    return 0;
}
  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星河依旧长明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值