luoguP2173 [ZJOI2012]网络 LCT

链接

luogu

思路

颜色很少,开10个lct分别维护

if (Hash.count(make_pair(u, v)) && Hash[make_pair(u, v)] == col) {puts("Success.");continue;}

这一行的代码调了半天。

代码

#include <bits/stdc++.h>
#define ls c[x][0]
#define rs c[x][1]
using namespace std;
const int N = 5e4 + 7;
int read() {
    int x = 0, f = 1; char s = getchar();
    for (;s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
    for (;s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
    return x * f;
}
map<pair<int, int>, int> Hash;
int n, m, c, k, w[N];
struct LCT {
    int ru[N];
    int f[N], c[N][2], ma[N], stak[N], lazy[N];
    bool isroot(int x) {return c[f[x]][0] == x || c[f[x]][1] == x;}
    void pushup(int x) {ma[x] = max(max(ma[ls], ma[rs]), w[x]);}
    void tag(int x){swap(ls,rs), lazy[x] ^= 1;}
    void pushdown(int x) {
        if (lazy[x]) {
            if (ls) tag(ls);
            if (rs) tag(rs);
            lazy[x] ^= 1;
        }
    }
    void rotate(int x) {
        int y = f[x], z = f[y], k = c[y][1] == x, w = c[x][!k];
        if (isroot(y)) c[z][c[z][1] == y] = x;
        c[x][!k] = y;
        c[y][k] = w;
        if (w) f[w] = y;
        f[x] = z;
        f[y] = x;
        pushup(y);
    }
    void splay(int x) {
        int y = x, z = 0;
        stak[++z] = y;
        while (isroot(y)) stak[++z] = y = f[y];
        while (z) pushdown(stak[z--]);
        while (isroot(x)) {
            y = f[x], z = f[y];
            if (isroot(y)) rotate((c[y][0] == x)^(c[z][0] == y) ? x : y);
            rotate(x);
        }
        pushup(x);
    }
    void access(int x) {
        for (int y = 0; x;x = f[y = x])
            splay(x), rs = y, pushup(x);
    }
    void makeroot(int x) {
        access(x), splay(x);
        tag(x);
    }
    int findroot(int x) {
        access(x), splay(x);
        while(ls) pushdown(x), x = ls;
        return x;
    }
    void split(int x, int y) {
        makeroot(x), access(y), splay(y);
    }
    void link(int x, int y) {
        makeroot(x);
        if (findroot(y) != x) f[x] = y;
    }
    void cut(int x, int y) {
        makeroot(x);
        if (findroot(y) == x && f[x] == y && !rs) {
            f[x] = c[y][0] = 0;
            pushup(y);
        }
    }
}lct[11];
int main() {
    // freopen("a.in", "r", stdin);
    int n = read(), m = read(), c = read(), k = read();
    for (int i = 1; i <= n; ++i) w[i] = read();
    for (int i = 1; i <= m; ++i) {
        int u = read(), v = read(), col = read();
        Hash[make_pair(u, v)] = col;
        Hash[make_pair(v, u)] = col;
        lct[col].link(u, v);
        lct[col].ru[u]++,lct[col].ru[v]++;
    }
    for (int i = 1; i <= k; ++i) {
        int opt = read();
        if (opt == 0) {
            int x = read(), y = read();
            w[x] = y;
            for (int j = 0; j <= c; ++j)
                lct[j].splay(x),lct[j].pushup(x);
        } else if (opt == 1) {
            int u = read(), v = read(), col = read();
            if (Hash.count(make_pair(u, v)) && Hash[make_pair(u, v)] == col) {puts("Success.");continue;}
            if (!Hash.count(make_pair(u, v))) {puts("No such edge.");continue;}
            if (lct[col].ru[u] >= 2 || lct[col].ru[v] >= 2) {puts("Error 1.");continue;}
            if (lct[col].findroot(u) == lct[col].findroot(v)) {puts("Error 2.");continue;}
            int old_col = Hash[make_pair(u,v)];
            Hash[make_pair(u, v)] = Hash[make_pair(v, u)] = col;
            lct[old_col].ru[u]--, lct[old_col].ru[v]--;
            lct[col].ru[u]++, lct[col].ru[v]++;
            lct[old_col].cut(u, v);
            lct[col].link(u, v);
            puts("Success.");
        } else {
            int col = read(), u = read(), v = read();
            if (lct[col].findroot(u) == lct[col].findroot(v)) {
                lct[col].split(u, v);
                printf("%d\n", lct[col].ma[v]);
            } else puts("-1");
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/dsrdsr/p/10960877.html

时间: 2024-08-05 03:35:19

luoguP2173 [ZJOI2012]网络 LCT的相关文章

ZJOI2012 网络——LCT相关题目

有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 在这个图上,你要支持以下三种操作: 修改一个节点的权值. 修改一条边的颜色. 查询由颜色c的边构成的图中,所有可能在节点u到节点v之间的简单路径上的节点的权值的最大值. https://daniu.luogu.org/problem/show?pid=2173 -by luogu 对每个颜色建LCT,对error

[ZJOI2012]网络

[ZJOI2012]网络 题目 思路 显然,这是一道lct裸题.因为颜色不多,所以对于每一种颜色的边我们都建一个lct即可.(我这里是用 (颜色×n+点的标号) 表示每一种颜色lct) 操作0 因为我们对于每一种颜色的边都建了一个lct所以,我们对于每一种颜色的边都update一次.(虽然很暴力,但跑得过) 操作1 1.其实对于判断边不存在的情况,我们可以用临接矩阵来存,开一个bool数组10000*10000 128M还是开得下的.这样节约了很多时间(其实是我懒得想其它方法判断). 2.错误1

[bzoj2816][ZJOI2012]网络(LCT,splay)

传送门 题解 话说以前还真没见过用LCT只维护一条链的……好像除了树点涂色那题…… 先看一下题目规定的两个性质 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 很明显了,同一种颜色肯定是由几条链组成的(虽然我根本没有发现) 然后又要查询边权和维护路径……直接上LCT吧 然后颜色数很少啊……每一个颜色开一个LCT好了 更改权值的话在每一个LCT上splay一下 修改颜色的话在原来的LCT中cut,新的LCT中link 查询路径直接split

AC日记——[ZJOI2012]网络 bzoj 2816

2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define ll long long int val[maxn]; struct LinkCutTreeType { int f[maxn],Max[maxn],ch[maxn][2],rev[maxn],sta[maxn],top,cnt[maxn]; void updata(int now) { Max[now]=va

洛谷 2173 [ZJOI2012]网络

[题解] 明显的LCT模板题,c种颜色就开c棵LCT好了.. 1 #include<cstdio> 2 #include<algorithm> 3 #define N 500010 4 #define C 20 5 #define rg register 6 #define ls (son[c][u][0]) 7 #define rs (son[c][u][1]) 8 using namespace std; 9 int n,m,c,k,opt,x,y,w,top; 10 int

P2173 [ZJOI2012]网络

\(\color{#0066ff}{ 题目描述 }\) 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 在这个图上,你要支持以下三种操作: 修改一个节点的权值. 修改一条边的颜色. 查询由颜色c的边构成的图中,所有可能在节点u到节点v之间的简单路径上的节点的权值的最大值. \(\color{#0066ff}{输入格式}\) 输入文件network.in的第一行

bzoj 2816: [ZJOI2012]网络(splay)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2816 [题意] 给定一个无向图,满足条件:从一个节点出发的同色边不超过2条,且不存在同色环.要求提供修改节点权值,修改边的颜色,查询同色边c构成的图中u->v路径上节点的最大权值. [思路] 根据满足的条件,可以判断同色的图构成了若干条一条链. 考虑使用splay维护这些链: 对于每个图上的点建C个splay结点.这里需要splay提供将结点u旋转到根的操作,所以需要维护一个fa指针

LCT总结

类比树剖,树剖是通过静态地把一棵树剖成若干条链然后用一种支持区间操作的数据结构维护(比如线段树.树状数组),而LCT是动态地去处理这个问题. 不少人都知道树剖用线段树维护,而LCT用\(splay\)维护.实际上同一条重链上的所有点才会被放在一棵\(splay\)中,而我们需要同时处理这若干条重链对应的若干棵\(splay\)之间的关系.因为一条重链上的每个点的深度互异,所以\(splay\)以深度\(dep\)为关键字. 我们规定一棵\(splay\)的根的\(fa\)为这条重链链顶节点在原树

【醒目】【业界偷懒】【Public】BZOJ题目一句话题解整理

就当是复习一下自己做过的题,顺便提供一个简要题解给大家看. 做题时候实在想不出来看一下一句话题解,可以有一个提示的作用又不至于一下子知道了全部浪费了一道题吧.. 部分题目(如我A过得大部分奶牛题)是别人拿我的账号做的,不提供题解. 可能会漏掉很多做过的题..因为可能点页数不小心点错了什么的 UPD.本来想把那些没写过但是知道题解的也写了..但是写完这些已经累死了QAQ 已AC的题目(数学题均不提供分析过程,公式): 1000:A+B 1001:平面图最小割,转对偶图最短路 1002:矩阵树定理,