洛谷 2173 BZOJ 2816 [ZJOI2012]网络

时间:2023-03-09 04:59:31
洛谷 2173 BZOJ 2816 [ZJOI2012]网络

洛谷 2173 BZOJ 2816 [ZJOI2012]网络

洛谷 2173 BZOJ 2816 [ZJOI2012]网络

洛谷 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]);
}
}
}