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

题面: http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf

思路:

因为c很小,我们可以建c棵树,然后跑LCT。

实现代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 3e5+;
const int inf = 0x3f3f3f3f; struct node{
int u,v;
};
int a[M];
int col[M][];
bool operator < (node a,node b){
if(a.u == b.u) return a.v < b.v;
return a.u < b.u;
}
map<node,int>mp;
struct lct{
int c[M][],fa[M],val[M],sum[M],rev[M],st[M];
inline void up(int x){
int l = c[x][],r = c[x][];
sum[x] = max(max(sum[l],sum[r]),val[x]);
} inline void pushrev(int x){
int t = c[x][];
c[x][] = c[x][]; c[x][] = t;
rev[x] ^= ;
} inline void pushdown(int x){
if(rev[x]){
int l = c[x][],r = c[x][];
if(l) pushrev(l);
if(r) pushrev(r);
rev[x] = ;
}
} inline bool nroot(int x){ //判断一个点是否为一个splay的根
return c[fa[x]][]==x||c[fa[x]][] == x;
} inline void rotate(int x){
int y = fa[x],z = fa[y],k = c[y][] == x;
int w = c[x][!k];
if(nroot(y)) c[z][c[z][]==y]=x;
c[x][!k] = y; c[y][k] = w;
if(w) fa[w] = y; fa[y] = x; fa[x] = z;
up(y);
} inline void splay(int x){
int y = x,z = ;
st[++z] = y;
while(nroot(y)) st[++z] = y = fa[y];
while(z) pushdown(st[z--]);
while(nroot(x)){
y = fa[x];z = fa[y];
if(nroot(y))
rotate((c[y][]==x)^(c[z][]==y)?x:y);
rotate(x);
}
up(x);
} //打通根节点到指定节点的实链,使得一条中序遍历从根开始以指定点结束的splay出现
inline void access(int x){
for(int y = ;x;y = x,x = fa[x])
splay(x),c[x][]=y,up(x);
} inline void makeroot(int x){ //换根,让指定点成为原树的根
access(x); splay(x); pushrev(x);
} inline int findroot(int x){ //寻找x所在原树的树根
access(x); splay(x);
while(c[x][]) pushdown(x),x = c[x][];
splay(x);
return x;
} inline void split(int x,int y){ //拉出x-y的路径成为一个splay
makeroot(x); access(y); splay(y);
} inline void cut(int x,int y){ //断开边
makeroot(x);
if(findroot(y) == x&&fa[y] == x&&!c[y][]){
fa[y] = c[x][] = ;
up(x);
}
} inline void link(int x,int y){ //连接边
makeroot(x);
if(findroot(y)!=x) fa[x] = y;
}
}LCT[];
int main()
{
int n,m,c,k;
scanf("%d%d%d%d",&n,&m,&c,&k);
for(int i = ;i <= n;i ++) scanf("%d",&a[i]);
for(int i = ;i <= n;i ++){
for(int j = ;j <= c;j ++){
LCT[j].val[i] = a[i];
}
}
int u,v,w;
for(int i = ;i <= m;i ++){
scanf("%d%d%d",&u,&v,&w); w++;
mp[(node){u,v}] = w;
mp[(node){v,u}] = w;
col[u][w]++; col[v][w]++;
LCT[w].link(u,v);
}
int op,x,y;
while(k--){
scanf("%d",&op);
if(op == ){
scanf("%d%d",&x,&y); a[x] = y;
for(int i = ;i <= c;i ++){
LCT[i].splay(x);
LCT[i].val[x] = a[x];
}
}
else if(op == ){
scanf("%d%d%d",&u,&v,&w); w++;
int f = mp[(node){u,v}];
if(!f) {
printf("No such edge.\n");
continue;
}
if(f == w){
printf("Success.\n");
continue;
}
if(col[u][w]>||col[v][w]>){
printf("Error 1.\n"); continue;
}
if(LCT[w].findroot(u)==LCT[w].findroot(v)){
printf("Error 2.\n"); continue;
}
col[u][f]--; col[v][f]--;
col[u][w]++; col[v][w]++;
mp[(node){u,v}] = w;
mp[(node){v,u}] = w;
LCT[f].cut(u,v);
LCT[w].link(u,v);
printf("Success.\n");
}
else{
scanf("%d%d%d",&w,&u,&v); w++;
if(LCT[w].findroot(u)!=LCT[w].findroot(v)){
printf("-1\n"); continue;
}
LCT[w].split(u,v);
printf("%d\n",LCT[w].sum[v]);
}
}
}

bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)的更多相关文章

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

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

  2. 【刷题】BZOJ 2816 [ZJOI2012]网络

    Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf Solution 维护树上联通块的信息,支持动态加边删边 LCT 总共 ...

  3. bzoj 2816: [ZJOI2012]网络(splay)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2816 [题意] 给定一个无向图,满足条件:从一个节点出发的同色边不超过2条,且不存在同 ...

  4. 洛谷 2173 BZOJ 2816 [ZJOI2012]网络

    [题解] 明显的LCT模板题,c种颜色就开c棵LCT好了.. #include<cstdio> #include<algorithm> #define N 100010 #de ...

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

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

  6. Luogu 2173 [ZJOI2012]网络 - LCT

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

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

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

  8. luoguP2173 [ZJOI2012]网络 LCT

    链接 luogu 思路 颜色很少,开10个lct分别维护 if (Hash.count(make_pair(u, v)) && Hash[make_pair(u, v)] == col ...

  9. 2816: [ZJOI2012]网络

    传送们 把一个点拆成c个即可 浪费时间的水题... //Achen #include<algorithm> #include<iostream> #include<cst ...

随机推荐

  1. jsp+servlet include引入文件指令

    1.index.jsp为首页 <%@ page contentType="text/html;charset=UTF-8" import="java.util.*& ...

  2. Sql server 2014 数据库还原奇异现象

    用A库来还原B库   对正在使用的B库执行还原,还原时修改数据库名称,还原出错,提示数据库正在使用.删除B库,仍然提示正在使用,感觉像僵尸     重启SQL SERVER,因B库已删除,在A库上点击 ...

  3. weblogic Patch

    How to Apply WLS Patch on Weblogic Integrated with OEM Getting "Main Thread" Java.lang.Out ...

  4. python读取txt文件最后一行(文件大+文件小)

    txt文件小 #coding:utf-8 ''' fname为所读xx.txt文件 输出为:文件第一行和最后一行 ''' fname = 'test.txt' with open(fname, 'r' ...

  5. Word Count作业

    Word Count作业 一.个人Gitee地址:https://gitee.com/Changyu-Guo 二.项目简介 该项目主要是模拟Linux上面的wc命令,基本要求如下: 命令格式: wc. ...

  6. Leaflet实现动态线路

    一.引用Leaflet脚本样式,和Leaflet Ant Path 插件 下载地址: Leaflet:https://leafletjs.com/download.html Leaflet Ant P ...

  7. AngularJS学习之旅—AngularJS Http(九)

    1.AngularJS XMLHttpRequest $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据. eg: // 简单的 GET 请求,可以改为 POST $htt ...

  8. c/c++ 继承与多态 友元与继承

    问题1:类B是类A的友元类,类C是类B的友元类,那么类C是类A的友元类吗?函数fun是类B的友元函数,那么fun是类A的友元函数吗? 都不是,友元关系不能传递. 问题2:类B是类A的友元类,类C是类B ...

  9. 【记录】IntelliJ IDEA—IDEA2018-2019激活

    摘要 最智能的java ide [有能力请支持正版]     1.将 0.0.0.0 account.jetbrains.com 和 0.0.0.0 www.jetbrains.com添加到 host ...

  10. SQL NULL 值

    NULL 值是遗漏的未知数据. 默认地,表的列可以存放 NULL 值. 本章讲解 IS NULL 和 IS NOT NULL 操作符. SQL NULL 值 如果表中的某个列是可选的,那么我们可以在不 ...