【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
    评论
题目描述 有一个 $n$ 个点的棋盘,每个点上有一个数字 $a_i$,你需要从 $(1,1)$ 走到 $(n,n)$,每次只能往右或往下走,每个格子只能经过一次,路径上的数字和为 $S$。定义一个点 $(x,y)$ 的权值为 $a_x+a_y$,求所有满足条件的路径中,所有点的权值和的最小值。 输入格式 第一行一个整数 $n$。 接下来 $n$ 行,每行 $n$ 个整数,表示棋盘上每个点的数字。 输出格式 输出一个整数,表示所有满足条件的路径中,所有点的权值和的最小值。 数据范围 $1\leq n\leq 300$ 输入样例 3 1 2 3 4 5 6 7 8 9 输出样例 25 算法1 (树形dp) $O(n^3)$ 我们可以先将所有点的权值求出来,然后将其看作是一个有权值的图,问题就转化为了在这个图中求从 $(1,1)$ 到 $(n,n)$ 的所有路径中,所有点的权值和的最小值。 我们可以使用树形dp来解决这个问题,具体来说,我们可以将这个图看作是一棵树,每个点的父节点是它的前驱或者后继,然后我们从根节点开始,依次向下遍历,对于每个节点,我们可以考虑它的两个儿子,如果它的两个儿子都被遍历过了,那么我们就可以计算出从它的左儿子到它的右儿子的路径中,所有点的权值和的最小值,然后再将这个值加上当前节点的权值,就可以得到从根节点到当前节点的路径中,所有点的权值和的最小值。 时间复杂度 树形dp的时间复杂度是 $O(n^3)$。 C++ 代码 算法2 (动态规划) $O(n^3)$ 我们可以使用动态规划来解决这个问题,具体来说,我们可以定义 $f(i,j,s)$ 表示从 $(1,1)$ 到 $(i,j)$ 的所有路径中,所有点的权值和为 $s$ 的最小值,那么我们就可以得到如下的状态转移方程: $$ f(i,j,s)=\min\{f(i-1,j,s-a_{i,j}),f(i,j-1,s-a_{i,j})\} $$ 其中 $a_{i,j}$ 表示点 $(i,j)$ 的权值。 时间复杂度 动态规划的时间复杂度是 $O(n^3)$。 C++ 代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值