【BZOJ2816】【ZJOI2012】网络(LCT)

90 篇文章 0 订阅
11 篇文章 0 订阅

Description

有一个无向图G,每个点有个权值,每条边有一个颜色。这个无向图满足以下两个条件:

  1. 对于任意节点连出去的边中,相同颜色的边不超过两条。

  2. 图中不存在同色的环,同色的环指相同颜色的边构成的环。

在这个图上,你要支持以下三种操作:

  1. 修改一个节点的权值。

  2. 修改一条边的颜色。

  3. 查询由颜色c的边构成的图中,所有可能在节点u到节点v之间的简单路径上的节点的权值的最大值。

Solution

对于每种颜色建一个LCT、直接维护即可。

有一些细节需要注意,比如在判断两个点 x,y x , y 是否间接相连时,先在LCT中分离出该链,然后不仅要满足 y y 的左子节点是x还要满足 x x <script type="math/tex" id="MathJax-Element-91">x</script>没有右子节点

Code

/************************************************
 * Au: Hany01
 * Date: Mar 27th, 2018
 * Prob: [BZOJ2816][ZJOI2012] 网络
 * Email: hany01@foxmail.com
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 10005, maxm = 100005, maxc = 10;

int n, m, c, k, val[maxn], deg[maxc][maxn];

struct LCT
{
    int fa[maxn], ch[maxn][2], rev[maxn], mx[maxn];

#define dir(u) (ch[fa[u]][1] == u)
#define isrt(u) (ch[fa[u]][0] != u && ch[fa[u]][1] != u)

    inline void pushdown(int o) {
        if (rev[o]) rev[o] = 0, rev[ch[o][1]] ^= 1, rev[ch[o][0]] ^= 1, swap(ch[o][0], ch[o][1]);
    }

    inline void maintain(int o) { mx[o] = max(mx[ch[o][0]], max(mx[ch[o][1]], val[o])); }

    inline void rotate(int u)
    {
        int f = fa[u], gf = fa[f], d = dir(u);
        ch[f][d] = ch[u][d ^ 1], fa[ch[u][d ^ 1]] = f;
        fa[u] = gf;
        if (!isrt(f)) ch[gf][dir(f)] = u;
        ch[u][d ^ 1] = f, fa[f] = u;
        maintain(f), maintain(u);
    }

    int stk[maxn], top;
    inline void splay(int u) {
        stk[top = 1] = u;
        for (register int t = u; !isrt(t); t = fa[t]) stk[++ top] = fa[t];
        while (top) pushdown(stk[top --]);
        for ( ; !isrt(u); rotate(u)) if (!isrt(fa[u])) rotate(dir(u) == dir(fa[u]) ? fa[u] : u);
    }

    inline void access(int u) {
        for (register int t = 0; u; t = u, u = fa[u]) splay(u), ch[u][1] = t, maintain(u);
    }

    inline void makeroot(int u) { access(u), splay(u), rev[u] ^= 1; }

    inline int findroot(int x) {
        access(x), splay(x);
        while (ch[x][0]) x = ch[x][0];
        return x;
    }

    inline bool link(int x, int y) {
        if (findroot(x) != findroot(y)) {
            makeroot(x), fa[x] = y;
            return 1;
        } return 0;
    }

    inline bool cut(int x, int y) {
        makeroot(x), access(y), splay(y);
        if (ch[y][0] == x) { ch[y][0] = fa[x] = 0; return 1; }
        return 0;
    }

    inline int distance(int x, int y) {
        if (findroot(x) != findroot(y)) return -1;
        return makeroot(x), access(y), splay(y), mx[y];
    }

}lct[maxc];

inline void update(int x, int y, int z)
{
    rep(i, c) if (lct[i].findroot(x) == lct[i].findroot(y)) {
        lct[i].makeroot(x), lct[i].access(y), lct[i].splay(y);
        if (lct[i].ch[y][0] != x || lct[i].ch[x][1]) continue;
        if (i == z) puts("Success.");
        else if (deg[z][x] > 1 || deg[z][y] > 1) puts("Error 1.");
        else if (lct[z].findroot(x) == lct[z].findroot(y)) puts("Error 2.");
        else lct[i].cut(x, y), lct[z].link(x, y), -- deg[i][x], -- deg[i][y], ++ deg[z][x], ++ deg[z][y], puts("Success.");
        return ;
    }
    puts("No such edge.");
}

int main()
{
#ifdef hany01
    File("bzoj2816");
#endif

    static int uu, vv, ww;
    n = read(), m = read(), c = read(), k = read();
    For(i, 1, n) {
        val[i] = read();
        rep(j, c) lct[j].mx[i] = val[i];
    }
    For(i, 1, m)
        uu = read(), vv = read(), ww = read(),
        lct[ww].link(uu, vv), ++ deg[ww][uu], ++ deg[ww][vv];

    while (k --)
    {
        register int opt = read(), x = read(), y = read();
        if (!opt) {
            val[x] = y;
            rep(j, c) lct[j].access(x), lct[j].splay(x), lct[j].maintain(x);
        } else if (opt == 1) update(x, y, read());
        else printf("%d\n", lct[x].distance(y, read()));
    }

    return 0;
}
//病起多情白日迟,强来庭下探花期。
//雪消池馆初春后,人倚栏杆欲暮时。
//乱蝶狂蜂俱有意,兔葵燕麦自无知。
//池边垂柳腰支活,折尽长条为寄谁?
//     --吕本中《春日即事》
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值