[题目链接]

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. zerorpc的安装

    1.简介及安装 rpc使构建分布式系统简单许多,在云计算的实现中有很广泛的应用 rpc可以是异步的 python实现rpc,可以使用标准库里的SimpleXMLRPCServer,另外zerorpc是 ...

  2. CNN网络--VGGNet

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

  3. GDB调试动态链接库

    http://cyukang.com/2012/06/25/gdb-with-libso.html http://cyukang.com/2011/05/06/valgrind.html

  4. JavaScript-4.7-friendly_table---ShinePans

    <html> <head> <meta http-equiv="content-type" content="text/html;chars ...

  5. 第14章8节《MonkeyRunner源代码剖析》 HierarchyViewer实现原理-获取控件列表并建立控件树

    在上几节的描写叙述中,我们把HierarchyViewer初始化好.也把ViewServer给装备好了.那如今距离获得一个控件去操作它是万事具备仅仅欠东风了,欠了那一股春风了?欠了的是建立控件树这个东 ...

  6. sql quer

    SELECT (SELECT COUNT (sysid) FROM FwInvConsumable WHERE parentref = g.sysid AND (ns.state = 'Invento ...

  7. Android 中View仅仅能接收到ACTION_DOWN无法接收ACTION_MOVE和ACTION_UP解决的方法

    昨天晚上调试了一晚上,在LinearLayout上接收屏幕动作,可是出现了问题, 以下的代码是本人调的代码 </pre><pre name="code" clas ...

  8. kubernetes调度之污点(taint)和容忍(toleration)

    系列目录 节点亲和性(affinity),是节点的一种属性,让符合条件的pod亲附于它(倾向于或者硬性要求).污点是一种相反的行为,它会使pod抗拒此节点(即pod调度的时候不被调度到此节点) 污点和 ...

  9. HBase写请求分析

    HBase作为分布式NoSQL数据库系统,不单支持宽列表.而且对于随机读写来说也具有较高的性能.在高性能的随机读写事务的同一时候.HBase也能保持事务的一致性. 眼下HBase仅仅支持行级别的事务一 ...

  10. 通过代码实现自动判断是手机端还是PC端跳转

    <!-- 2017/09/13 跳转手机页面 start by 小鬼PSer --> <meta name="mobile-agent" content=&quo ...