链接

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;
}

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

  1. bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...

  2. BZOJ.2816.[ZJOI2012]网络(LCT)

    题目链接 BZOJ 洛谷 对每种颜色维护一个LCT,保存点之间的连接关系. 修改权值A[x]和所有Max[x]都要改: 修改边的颜色先枚举所有颜色,看是否在某种颜色中有边,然后断开.(枚举一遍就行啊 ...

  3. Luogu 2173 [ZJOI2012]网络 - LCT

    Solution $LCT$ 直接上$QuQ$ 注意$cut$ 完 需要 $d[u + c * N]--$ 再  $link$,  不然会输出Error 1的哦 Code #include<cs ...

  4. ZJOI2012 网络——LCT相关题目

    有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 在这个图上,你 ...

  5. BZOJ2816:[ZJOI2012]网络(LCT)

    Description 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构 ...

  6. 洛谷 P2173 [ZJOI2012]网络 解题报告

    P2173 [ZJOI2012]网络 题目描述 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环, ...

  7. ZJOI2012网络 题解报告【LCT】

    题目描述 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 在这 ...

  8. 洛谷P2173 [ZJOI2012]网络(10棵lct与瞎jb暴力)

    有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 在这个图上,你 ...

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

    传送门 题解 话说以前还真没见过用LCT只维护一条链的……好像除了树点涂色那题…… 先看一下题目规定的两个性质 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜 ...

随机推荐

  1. Kubernetes ingress 上传文件大小限制

    1.文件太大 413:Request Entity Too Large 创建 ingress 时添加 annotations(注释) metadata: annotations: nginx.ingr ...

  2. Vue.js 源码分析(十二) 基础篇 组件详解

    组件是可复用的Vue实例,一个组件本质上是一个拥有预定义选项的一个Vue实例,组件和组件之间通过一些属性进行联系. 组件有两种注册方式,分别是全局注册和局部注册,前者通过Vue.component() ...

  3. Sitecore 营销分类

    分类法是一种分层结构,可用于识别和组织信息.在Sitecore体验平台中,您可以使用分类法对营销活动进行分类,例如广告,目标和活动.您可以在市场营销控制面板中将分类标签应用于这些项目. 营销分类的四种 ...

  4. 基础知识---IEnumerable、ICollection、IList、IQueryable

    一.定义 IEnumerable public interface IEnumerable<out T> : IEnumerable ICollection public interfac ...

  5. 利用kibana学习 elasticsearch restful api (DSL)

    利用kibana学习 elasticsearch restful api (DSL) 1.了解elasticsearch基本概念Index: databaseType: tableDocument: ...

  6. 【JVM】【linux】linux上执行jmap命令查看JVM内存使用情况,报错:sun.jvm.hotspot.debugger.NoSuchSymbolException: Could not find symbol "gHotSpotVMTypes" in any of the known library name

    运行命令: jmap -heap 报错如下: Attaching to process ID , please wait... sun.jvm.hotspot.debugger.NoSuchSymbo ...

  7. 记WinForm中WebBrowser相关的俩个问题

    问题一:如何不让WebBrowser中弹出“安全警告” 当链接https网址时,IE会自动弹出上图中的窗口. 关闭窗口的具体思路如下: 使用WebBrowser加载中/加载完毕后触发的事件处理程序,在 ...

  8. 我是如何一步步编码完成万仓网ERP系统的(六)产品库设计 2.百度Ueditor编辑器

    https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...

  9. eclipse对Java项目进行单元测试

    一.右键项目名->buildpath->configure buildpath->add library->junit->选择版本(以4为例)->apply-> ...

  10. Jenkins 显示语言-英文切换中文

    1.进入插件管理 搜索 Locale 插件并进行安装 2.插件安装成功后进入系统设置 3.在 Locale 中 Default Language 字段填入 zh_CN 并且勾选 Ignore brow ...