洛谷 2173 BZOJ 2816 [ZJOI2012]网络
【题解】
明显的LCT模板题,c种颜色就开c棵LCT好了。。
#include<cstdio>
#include<algorithm>
#define N 100010
#define C 11
#define rg register
#define ls (son[c][u][0])
#define rs (son[c][u][1])
using namespace std;
int n,m,c,k,opt,x,y,w,top;
int cnt[C][N],fa[C][N],son[C][N][],val[N],mx[C][N],rev[C][N],st[N],to[C][N][];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline bool isroot(int c,int u){
return son[c][fa[c][u]][]!=u&&son[c][fa[c][u]][]!=u;
}
inline bool which(int c,int u){
return son[c][fa[c][u]][]==u;
}
inline void pushup(int c,int u){
mx[c][u]=max(max(mx[c][ls],mx[c][rs]),val[u]);
}
inline void pushdown(int c,int u){
rev[c][ls]^=; rev[c][rs]^=; rev[c][u]=; swap(ls,rs);
}
void rotate(int c,int u){
int f=fa[c][u],gf=fa[c][f],wh=which(c,u);
if(!isroot(c,f)) son[c][gf][which(c,f)]=u;
fa[c][u]=fa[c][f]; fa[c][f]=u; fa[c][son[c][u][wh^]]=f;
son[c][f][wh]=son[c][u][wh^]; son[c][u][wh^]=f;
pushup(c,f); pushup(c,u);
}
inline void splay(int c,int u){
st[top=]=u;
for(rg int i=u;!isroot(c,i);i=fa[c][i]) st[++top]=fa[c][i];
for(rg int i=top;i;i--) if(rev[c][st[i]]) pushdown(c,st[i]);
while(!isroot(c,u)){
if(!isroot(c,fa[c][u])) rotate(c,which(c,u)==which(c,fa[c][u])?fa[c][u]:u);
rotate(c,u);
}
}
inline void access(int c,int u){
for(rg int s=;u;s=u,u=fa[c][u]) splay(c,u),son[c][u][]=s,pushup(c,u);
}
inline void makeroot(int c,int u){
access(c,u); splay(c,u); rev[c][u]^=;
}
inline int find(int c,int u){
access(c,u); splay(c,u);
while(ls) u=ls; return u;
}
inline void split(int c,int x,int y){
makeroot(c,x); access(c,y); splay(c,y);
}
inline void link(int c,int x,int y){
cnt[c][x]++; cnt[c][y]++;
if(to[c][x][]==-) to[c][x][]=y; else to[c][x][]=y;
if(to[c][y][]==-) to[c][y][]=x; else to[c][y][]=x;
makeroot(c,x); fa[c][x]=y;
}
inline void cut(int c,int x,int y){
split(c,x,y); int t=son[c][y][];
if(!son[c][t][]&&t==x){
son[c][y][]=,fa[c][x]=,cnt[c][x]--,cnt[c][y]--;
if(to[c][x][]==y) to[c][x][]=-; else to[c][x][]=-;
if(to[c][y][]==x) to[c][y][]=-; else to[c][y][]=-;
}
else{
while(son[c][t][]) t=son[c][t][];
if(t==x){
son[c][fa[c][t]][]=,fa[c][x]=,cnt[c][x]--,cnt[c][y]--;
if(to[c][x][]==y) to[c][x][]=-; else to[c][x][]=-;
if(to[c][y][]==x) to[c][y][]=-; else to[c][y][]=-;
}
}
}
inline bool haveedge(int c,int x,int y){
split(c,x,y); int t=son[c][y][];
if(!son[c][t][]&&t==x) return ;
else{
while(son[c][t][]) t=son[c][t][];
if(t==x) return ;
}
return ;
}
int main(){
n=read(); m=read(); c=read()-; k=read();
for(rg int i=;i<=n;i++){
val[i]=read();
for(rg int j=;j<=c;j++) mx[j][i]=val[i];
}
for(rg int i=;i<=m;i++){
x=read(); y=read(); w=read();
link(w,x,y);
}
while(k--){
if((opt=read())==){
x=read(),y=read();
for(rg int i=;i<=c;i++) access(i,x),splay(i,x);
val[x]=y;
for(rg int i=;i<=c;i++) pushup(i,x);
}
if(opt==){
x=read(),y=read(),w=read(); bool linked=; int lastcol=;
for(rg int i=;i<=c;i++)if(haveedge(i,x,y)){
linked=; lastcol=i; break;
}
// for(rg int i=0;i<=c;i++)if(to[i][x][0]==y||to[i][x][1]==y){
// linked=1; lastcol=i; break;
// }
if(lastcol==w&&linked){
puts("Success."); continue;
}
if(!linked){
puts("No such edge."); continue;
}
if(cnt[w][x]>=||cnt[w][y]>=){
puts("Error 1."); continue;
}
if(find(w,x)==find(w,y)){
puts("Error 2."); continue;
}
cut(lastcol,x,y); link(w,x,y); puts("Success.");
}
if(opt==){
w=read(); x=read(); y=read();
if(find(w,x)!=find(w,y)){
puts("-1"); continue;
}
split(w,x,y); printf("%d\n",mx[w][y]);
}
}
}
洛谷 2173 BZOJ 2816 [ZJOI2012]网络的更多相关文章
- BZOJ.2816.[ZJOI2012]网络(LCT)
题目链接 BZOJ 洛谷 对每种颜色维护一个LCT,保存点之间的连接关系. 修改权值A[x]和所有Max[x]都要改: 修改边的颜色先枚举所有颜色,看是否在某种颜色中有边,然后断开.(枚举一遍就行啊 ...
- bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...
- bzoj 2816: [ZJOI2012]网络(splay)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2816 [题意] 给定一个无向图,满足条件:从一个节点出发的同色边不超过2条,且不存在同 ...
- 【刷题】BZOJ 2816 [ZJOI2012]网络
Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf Solution 维护树上联通块的信息,支持动态加边删边 LCT 总共 ...
- 洛谷 P3307: bzoj 3202: [SDOI2013] 项链
题目传送门:洛谷P3307.这题在bzoj上是权限题. 题意简述: 这题分为两个部分: ① 有一些珠子,每个珠子可以看成一个无序三元组.三元组要满足三个数都在$1$到$m$之间,并且三个数互质,两个珠 ...
- 洛谷 4106 / bzoj 3614 [HEOI2014]逻辑翻译——思路+类似FWT
题目:https://www.luogu.org/problemnew/show/P4106 https://www.lydsy.com/JudgeOnline/problem.php?id=3614 ...
- 洛谷 P3332 BZOJ 3110 [ZJOI2013]K大数查询
题目链接 洛谷 bzoj 题解 整体二分 Code #include<bits/stdc++.h> #define LL long long #define RG register usi ...
- 洛谷 P2486 BZOJ 2243 [SDOI2011]染色
题目描述 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221” ...
- 洛谷 P2827 BZOJ 4721 UOJ #264 蚯蚓
题目描述 本题中,我们将用符号表示对c向下取整,例如:. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. 蛐蛐国里现在共有n只蚯蚓(n为正整数).每只 ...
随机推荐
- AT2004 Anticube
https://www.zybuluo.com/ysner/note/1304774 题面 给定\(n\)个数\(s_i\),要求从中选出最多的数,满足任意两个数之积都不是完全立方数. \(n\leq ...
- bzoj4195 [Noi2015]程序自动分析——并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4195 突然在这道大水题上WA了半天... 思路很简单,离线处理询问,先把 = 的都加到并查集 ...
- selenium3.4.3 + python3.6 + HTMLTestRunner0.8.0
HTMLTestRunner下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html.该页面下载的HTMLTestRunner默认支持pytho ...
- E20180119
Foundation n. 基础; 地基; 粉底; 基金(会); hybrid n. 杂种; 杂交生成的生物体; 混合物; 混合词; adj. 混合的; 杂种的;
- Python网咯爬虫 — Scrapy框架应用
Scrapy框架 Scrapy是一个高级的爬虫框架,它不仅包括了爬虫的特征,还可以方便地将爬虫数据保存到CSV.Json等文件中. Scrapy用途广泛,可以用于数据挖掘.监测 ...
- AtCoder Regular Contest 099 C~E
C - Minimization 枚举就可以了 因为最后一定会变成1,所以第一次操作的区间就包含1会比较优,然后枚举1在第一次操作区间里排第几个取min即可 #include<iostream& ...
- P3402 【模板】可持久化并查集
传送门 //minamoto #include<bits/stdc++.h> using namespace std; #define getc() (p1==p2&&(p ...
- codechef: ADAROKS2 ,Ada Rooks 2
又是道原题... (HDU 6313 Hack It , 多校 ACM 里面的题) 题目说构造一个 n * n 矩阵,染色点不得构成矩形...然后染色点个数至少 8 * n 然后我们生成一个数 m , ...
- 水题 CodeForces 137A Postcards and photos
题目传送门 /* 水! */ #include <cstdio> #include <cstring> #include <algorithm> using nam ...
- EasyUI系列学习(十一)-Accordion(分类)
一.加载 1.class加载 <div class="easyui-accordion" style="width:300px;height:200px" ...