[题目链接]

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

[算法]

对每种颜色的边建一棵LCT , 维护联通性即可

时间复杂度 : O(C * NlogN ^ 2)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
const int MAXC = ;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull; int n , m , c , k;
int u[MAXN] , v[MAXN] , w[MAXN] , val[MAXN] , cnt[MAXC][MAXN];
map< pair<int , int> , int> mp; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} struct Link_Cut_Tree
{
struct Node
{
int father , son[] , value , mx;
bool tag;
} a[MAXN];
inline void init()
{
for (int i = ; i <= n; i++)
{
a[i].father = ;
a[i].son[] = a[i].son[] = ;
a[i].value = a[i].mx = val[i];
a[i].tag = false;
}
}
inline void pushdown(int x)
{
if (a[x].tag)
{
swap(a[x].son[] , a[x].son[]);
a[a[x].son[]].tag ^= ;
a[a[x].son[]].tag ^= ;
a[x].tag = false;
}
}
inline void update(int x)
{
a[x].mx = a[x].value;
if (a[x].son[]) chkmax(a[x].mx , a[a[x].son[]].mx);
if (a[x].son[]) chkmax(a[x].mx , a[a[x].son[]].mx);
}
inline bool get(int x)
{
pushdown(x);
return a[a[x].father].son[] == x;
}
inline bool nroot(int x)
{
return a[a[x].father].son[] == x || a[a[x].father].son[] == x;
}
inline void rotate(int x)
{
int f = a[x].father , g = a[f].father;
int tmpx = get(x) , tmpf = get(f);
int w = a[x].son[tmpx ^ ];
if (nroot(f)) a[g].son[tmpf] = x;
a[x].son[tmpx ^ ] = f;
a[f].son[tmpx] = w;
if (w) a[w].father = f;
a[f].father = x;
a[x].father = g;
update(f);
}
inline void splay(int x)
{
int y = x , z = ;
static int st[MAXN];
st[++z] = y;
while (nroot(y)) st[++z] = y = a[y].father;
while (z) pushdown(st[z--]);
while (nroot(x))
{
int y = a[x].father , z = a[y].father;
if (nroot(y))
rotate((a[z].son[] == y) ^ (a[y].son[] == x) ? x : y);
rotate(x);
}
update(x);
}
inline void access(int x)
{
for (int y = ; x; x = a[y = x].father)
{
splay(x);
a[x].son[] = y;
update(x);
}
}
inline void make_root(int x)
{
access(x);
splay(x);
a[x].tag ^= ;
pushdown(x);
}
inline void link(int x , int y)
{
make_root(x);
if (find_root(y) != x) a[x].father = y;
}
inline int find_root(int x)
{
access(x);
splay(x);
while (a[x].son[])
{
pushdown(x);
x = a[x].son[];
}
return x;
}
inline void cut(int x , int y)
{
make_root(x);
if (find_root(y) == x && a[x].father == y && !a[x].son[])
{
a[x].father = a[y].son[] = ;
update(y);
}
}
inline void split(int x , int y)
{
make_root(x);
access(y);
splay(y);
}
inline void modify(int x , int y)
{
splay(x);
a[x].value = y;
update(x);
}
inline bool connected(int x , int y)
{
return (find_root(x) == find_root(y));
}
inline int query(int x , int y)
{
split(x , y);
return a[y].mx;
}
} T[MAXC]; int main()
{ read(n); read(m); read(c); read(k);
for (int i = ; i <= n; i++) read(val[i]);
for (int i = ; i < c; i++) T[i].init();
for (int i = ; i <= m; i++)
{
read(u[i]); read(v[i]); read(w[i]);
mp[make_pair(u[i] , v[i])] = mp[make_pair(v[i] , u[i])] = w[i];
T[w[i]].link(u[i] , v[i]);
++cnt[w[i]][u[i]]; ++cnt[w[i]][v[i]];
}
for (int i = ; i <= k; i++)
{
int type;
read(type);
if (type == )
{
int x , y;
read(x); read(y);
for (int i = ; i < c; i++) T[i].modify(x , y);
} else if (type == )
{
int u , v , w;
read(u); read(v); read(w);
if (!mp.count(make_pair(u , v)))
{
printf("No such edge.\n");
continue;
} else
{
int value = mp[make_pair(u , v)];
if (value == w)
{
printf("Success.\n");
continue;
}
if (cnt[w][u] >= || cnt[w][v] >= )
{
printf("Error 1.\n");
continue;
}
if (T[w].connected(u , v))
{
printf("Error 2.\n");
continue;
}
printf("Success.\n");
T[value].cut(u , v);
--cnt[value][u]; --cnt[value][v];
T[w].link(u , v);
mp[make_pair(u , v)] = mp[make_pair(v , u)] = w;
++cnt[w][u]; ++cnt[w][v];
}
} else
{
int w , u , v;
read(w); read(u); read(v);
if (!T[w].connected(u , v)) printf("-1\n");
else printf("%d\n" , T[w].query(u , v));
}
} return ; }

[ZJOI 2012] 网络的更多相关文章

  1. [ ZJOI 2010 ] 网络扩容

    \(\\\) Description 给定一张有向图,每条边都有一个容量 \(C\) 和一个扩容费用 \(W\). 这里扩容费用是指将容量扩大 \(1\) 所需的费用.求: 在不扩容的情况下, \(1 ...

  2. 数学 ZJOI 2012 数列

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ; st ...

  3. [ZJOI 2012]灾难

    Description 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. 学过生 ...

  4. jdbc_odbc SQLserver 驱动安装及测试

    有2次被问到同一个问题,尽管博客园是.net的园子,我还是分享下吧.PS:我现在做的.net,以前学过点java.献丑了. ------------------ 原始邮件 -------------- ...

  5. Windows Server 2012 虚拟化实战:网络(二)

    关于Windows Server的虚拟化网络,前文描述了在操作系统层面上的出现的配置变化.其中的一些配置通过Windows Server提供的小工具即可实现,如网卡组的配置,而有些需要安装Window ...

  6. Windows Server 2012 虚拟化实战:网络(一)

    虚拟化对于计算的抽象,大家可能相对熟悉,也许都有在单机使用诸如Virtual PC或者Virtual Box的经验.使用的这些虚拟化软件的第一印象就是我们的CPU可以同时运行多套不同的操作系统,并且其 ...

  7. 网络安装archlinux(2012.8.20)笔记

    周末闲极无聊,把烂笔记本翻出来想装个Archlinux,发现USB不能启动,光驱也挂了,只好网络安装. 我先后试了两种安装方式,一种纯互联网启动,安装,一种局域网启动,再互联网安装.分别说说. 不管哪 ...

  8. Windows Server 2012 R2在桌面上显示计算机/网络图标

    原文 Windows Server 2012 R2在桌面上显示计算机/网络图标 从Windows2012开始,微软取消了服务器桌面个性化选项,如何重新调出配置界面,可以使用微软命令调出.具体方法如下: ...

  9. 第四章 Hyper-V 2012 R2 网络配置

      尼玛的我不高兴写了,所以下面的文档我直接把原来的pdf给转换出来,加了点自己的注解,我写的话会写自己觉得终于的章节. 在搭建虚拟化平台时,网络的虚拟化是一个非常重要的环节,如何保障网络的持续可用并 ...

随机推荐

  1. ASP复制文件

    <% dim fs,oldpath,newpath Set fs=Server.CreateObject("Scripting.FileSystemObject") oldp ...

  2. log4net菜鸟指南二----生成access和txt

    前言 有可能目标计算机缺少某些组件,导致无法生成access文件,或者打不开文件,这时txt文件就可以方便的使用了 一,标准的控制台程序输出日志到access <?xml version=&qu ...

  3. ssm 网页

    http://stackoverflow.com/questions/14545872/bean-named-xxx-must-be-of-typexxx-but-was-actually-of-ty ...

  4. CSS3中的动画效果-------Day72

    还记得么,在前面也曾实现过"仅仅用css让div动起来",还记得当时是怎么实现的么,是的,transition,针对的也比較局限,仅仅有旋转角度啊,长宽啊之类的,所以说,与其说是动 ...

  5. Java依照List内存储的对象的某个字段进行排序

    关键点:将List内存储的对象实现Comparable类.重写它的compareTo()方法就可以 Bean: package chc; public class StuVo implements C ...

  6. java 堆和栈一般理解

    栈与堆都是Java用来在Ram中存放数据的地方.与C++不同.Java自己主动管理栈和堆.程序猿不能直接地设置栈或堆.  Java的堆是一个执行时数据区,类的(对象从中分配空间.这些对象通过new.n ...

  7. SpringMVC学习(一):搭建SpringMVC-注解-非注解

    文章参考:http://www.cnblogs.com/Sinte-Beuve/p/5730553.html 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话pom.xml配置依 ...

  8. h5页面测试

    转自:http://www.blogjava.net/qileilove/archive/2014/07/24/416154.html?utm_source=tuicool&utm_mediu ...

  9. EasyDarwin开源流媒体云平台之语音对讲功能设计与实现

    本文由EasyDarwin开源团队成员Alex贡献:http://blog.csdn.net/cai6811376/article/details/52006958 EasyDarwin云平台一直在稳 ...

  10. 九度OJ 1141:Financial Management (财务管理) (平均数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:939 解决:489 题目描述: Larry graduated this year and finally has a job. He's ...