[题目链接]

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. inputclean插件的使用方法

    inputclean插件的使用方法 语言:javascript 是jquery插件, 目的:给文本框添加×,点击×,就可以清空文本框内容. 如图: 详细交互效果: 1,当文本框聚焦时,×永久显示,无论 ...

  2. 解决Sophos UTM 9防火墙上的“根分区填满”问题

    Resolving 'Root Partition Is Filling Up' Issue on Sophos UTM Firewall 收到“Sophos UTM 9”防火墻的“根分區填满”问题的 ...

  3. 【翻译自mos文章】当并行事务恢复进程在执行时,禁用并行事务恢复的方法

    当并行事务恢复进程在执行时,禁用并行事务恢复的方法 How to Disable Parallel Transaction Recovery When Parallel Txn Recovery is ...

  4. 无向图的点双连通分量(tarjan模板)

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...

  5. LinearLayout具体解释三:LayoutInflater创建View过程分析

    上次讲到以下这么一段代码,这段代码的作用就是解析xml文件成为view并显示到屏幕上的. @Override //设置contentview,也就是activity或fragment载入视图,即vie ...

  6. 怎样使用oracle 的DBMS_SQLTUNE package 来执行 Sql Tuning Advisor 进行sql 自己主动调优

     怎样使用oracle 的DBMS_SQLTUNE package 来执行 Sql Tuning Advisor 进行sql 自己主动调优 1>.这里简单举个样例来说明DBMS_SQLTUN ...

  7. 在CentOS 7上构建RAID5、LVM和SAMBAserver(5)——架设SAMBAserver

    在CentOS 7上构建RAID5.LVM和SAMBAserver(5)--架设SAMBAserver 6. 架设SAMBAserver 6.1. 预备 本节的任务是配置SAMBA服务,共享/home ...

  8. Ubuntu下安装libsvm

    在安装LibSVM前需要先装 python 和 gnuplot linux 一般都自带了python2.7,所以python的安装不再赘述 在 ubuntu 下安装 gnuplot 不能直接 sudo ...

  9. Jenkins系列之-—06 Ant构建

    一.Ant 简介&构建环境 Apache Ant 是由 Java 语言开发的工具 构建ant环境: 1). 安装jdk,设置JAVA_HOME ,PATH ,CLASS_PATH 2). 下载 ...

  10. vmware克隆一台机器后修改etho

    1 vi /etc/udev/rules.d/70-persistent-net.rules 2 注释NAME="eth0"的内容 3 将NAME="eth1" ...