XII.[ZJOI2012]网络
这题还以为有什么高端做法呢,一看\(C\leq 10\),这题就算结束了。
它的那个限制翻译成人话就是“无论何时,任何颜色的边总是构成一条条链”。然后换颜色就暴力连边断边即可。
代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,c,q,col[100100],val[10010];
map<pair<int,int>,int>mp;
struct Link_Cut_Tree{
#define lson t[x].ch[0]
#define rson t[x].ch[1]
int deg[100100];
struct node{
int ch[2],fa,val,mx;
bool rev;
}t[10010];
int identify(int x){
if(t[t[x].fa].ch[0]==x)return 0;
if(t[t[x].fa].ch[1]==x)return 1;
return -1;
}
void pushup(int x){
t[x].mx=t[x].val;
if(lson)t[x].mx=max(t[x].mx,t[lson].mx);
if(rson)t[x].mx=max(t[x].mx,t[rson].mx);
}
void REV(int x){
t[x].rev^=1,swap(lson,rson);
}
void pushdown(int x){
if(!t[x].rev)return;
if(lson)REV(lson);
if(rson)REV(rson);
t[x].rev=false;
}
void rotate(int x){
int y=t[x].fa;
int z=t[y].fa;
int dirx=identify(x);
int diry=identify(y);
int b=t[x].ch[!dirx];
if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
if(b)t[b].fa=y;t[y].ch[dirx]=b;
t[y].fa=x,t[x].ch[!dirx]=y;
pushup(y),pushup(x);
}
void pushall(int x){
if(identify(x)!=-1)pushall(t[x].fa);
pushdown(x);
}
void splay(int x){
pushall(x);
while(identify(x)!=-1){
int fa=t[x].fa;
if(identify(fa)==-1)rotate(x);
else if(identify(fa)==identify(x))rotate(fa),rotate(x);
else rotate(x),rotate(x);
}
}
void access(int x){
for(int y=0;x;x=t[y=x].fa)splay(x),rson=y,pushup(x);
}
void makeroot(int x){
access(x),splay(x),REV(x);
}
int findroot(int x){
access(x),splay(x);
pushdown(x);
while(lson)x=lson,pushdown(x);
splay(x);
return x;
}
int split(int x,int y){
if(findroot(x)!=findroot(y))return -1;
makeroot(x),access(y),splay(y);
return t[y].mx;
}
bool link(int x,int y){
if(findroot(x)==findroot(y))return false;
makeroot(x),t[x].fa=y;
return true;
}
void cut(int x,int y){
split(x,y),t[x].fa=t[y].ch[0]=0,pushup(y);
}
}LCT[10];
int main(){
scanf("%d%d%d%d",&n,&m,&c,&q);
for(int i=1;i<=n;i++){
scanf("%d",&val[i]);
for(int j=0;j<c;j++)LCT[j].t[i].val=LCT[j].t[i].mx=val[i];
}
for(int i=1,x,y;i<=m;i++){
scanf("%d%d%d",&x,&y,&col[i]);
if(x>y)swap(x,y);
mp[make_pair(x,y)]=i;
LCT[col[i]].link(x,y);
LCT[col[i]].deg[x]++,LCT[col[i]].deg[y]++;
}
for(int i=1,t1,t2,t3,t4;i<=q;i++){
scanf("%d",&t1);
if(t1==0){
scanf("%d%d",&t2,&t3),val[t2]=t3;
for(int j=0;j<c;j++)LCT[j].makeroot(t2),LCT[j].splay(t2),LCT[j].t[t2].val=t3,LCT[j].pushup(t2);
}
if(t1==1){
scanf("%d%d%d",&t2,&t3,&t4);
if(t2>t3)swap(t2,t3);
map<pair<int,int>,int>::iterator it=mp.find(make_pair(t2,t3));
if(it==mp.end()){puts("No such edge.");continue;}
if(col[it->second]==t4){puts("Success.");continue;}
if(LCT[t4].deg[t2]==2||LCT[t4].deg[t3]==2){puts("Error 1.");continue;}
if(!LCT[t4].link(t2,t3)){puts("Error 2.");continue;}
LCT[col[it->second]].cut(t2,t3);
LCT[col[it->second]].deg[t2]--,LCT[col[it->second]].deg[t3]--;
col[it->second]=t4;
LCT[t4].deg[t2]++,LCT[t4].deg[t3]++;
puts("Success.");
}
if(t1==2)scanf("%d%d%d",&t2,&t3,&t4),printf("%d\n",LCT[t2].split(t3,t4));
}
return 0;
}