[题目链接]

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. CNN网络--VGGNet

    Simonyan, Karen, and Andrew Zisserman. "Very deep convolutional networks for large-scale image ...

  2. hdu 4432 数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=4432 6分钟写的代码,一上午去调试,, 哎,一则题目没看懂就去写了,二则,哎,,恶心了.在坚持几天然后ACM退役 ...

  3. Swift----编程语言语法

    1   简单介绍 今天凌晨Apple刚刚公布了Swift编程语言,本文从其公布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS&a ...

  4. C语言-回溯例1

    回溯法解N皇后问题 1,代码分析: 使用一个一维数组表示皇后的位置 其中数组的下标表示皇后所在的行 数组元素的值表示皇后所在的列 这样设计的棋盘,所有皇后必定不在同一行 假设前n-1行的皇后已经按照规 ...

  5. 开始学习linux的一些疑问

    Linux - Unix环境高级编程(第三版) 代码编译 https://www.linuxidc.com/Linux/2011-08/41228.htm ftp://ftp1.linuxidc.co ...

  6. Machine Learning: 一部气势恢宏的人工智能发展史

    转载自:雷锋网 本文作者:陈圳 2016-09-12 09:46 导语:机器学习的从产生,发展,低潮和全盛的历史 雷锋网(公众号:雷锋网)按:本文作者DataCastle数据城堡,主要介绍了机器学习的 ...

  7. React机制浅析

    在写React代码时,避免不了提前想想页面的结构,当然这也属于HTML布局了,比如哪些组件里需要包含哪些组件.遂突发奇想,如果试着把子组件的render内容替换原组件,会是个啥? 比如拿 https: ...

  8. 数据结构基础之memset---有memset 抛出的int 和 char 之间的转换和字节对齐

    今天晚上,在做滤波算法时,里面用到很多float 和int 以及char 之间的类型强制转换,后面滤波完发现图片有些区域块,有过度曝光的白光,我就跟踪,以为是char 字符数字数据溢出问题,加了0-2 ...

  9. 使用C#解决部分Win8.1系统窗体每隔几秒失去焦点的问题

    使用了Win8.1 With Update 1后,发现重新启动系统后,当前激活的窗体总是每隔几秒失去焦点.过0.5~1秒焦点回来.导致输入无法正常工作,严重影响使用心情和效率. 在网上找了非常久,也没 ...

  10. hdu 1413 文件系统

    hdu   1413   文件系统         题目链接:pid=1413" target="_blank">http://acm.hdu.edu.cn/sho ...