题解:

很显然会发现对于每种颜色分开处理这是一颗树

然后就是裸的lct

有个坑就是判断操作1 可能颜色改成跟原先一样的

代码:

#include <bits/stdc++.h>
using namespace std;
#define N 11000
#define M 15
int n,m,c,kk;
int cnt[M][N],v[N];
struct lct{
int data[N],fa[N];
int leftson[N],rightson[N];
int count2[N];
bool rev[N];
void down(int x)
{
if (!rev[x]) return;
swap(leftson[x],rightson[x]); rev[x]=;
rev[leftson[x]]^=; rev[rightson[x]]^=;
}
void updata(int x)
{
down(x);
count2[x]=count2[leftson[x]]+count2[rightson[x]]+;
data[x]=max(v[x],max(data[leftson[x]],data[rightson[x]]));
}
bool pd(int x)
{
int y=fa[x];
if (leftson[y]!=x&&rightson[y]!=x) return false;
else return (true);
}
void rotate(int x,int y)
{
int father=fa[x];
if (y==)
{
rightson[father]=leftson[x];
if (leftson[x]) fa[leftson[x]]=father;
} else
{
leftson[father]=rightson[x];
if (rightson[x]) fa[rightson[x]]=father;
}
fa[x]=fa[father];
if (pd(father))
{
if (leftson[fa[x]]==father)
leftson[fa[x]]=x;
else rightson[fa[x]]=x;
}
fa[father]=x;
if (y==) leftson[x]=father;
else rightson[x]=father;
updata(father); updata(x);
}
void dfs(int x)
{
if (pd(x)) dfs(fa[x]);
down(x);
}
void splay(int x)
{
dfs(x);
int father=fa[x];
while (pd(x))
{
if (!pd(father))
{
if (x==leftson[father]) rotate(x,);
else rotate(x,);
} else
{
if (father==leftson[fa[father]])
if (x==leftson[father])
rotate(father,),rotate(x,);
else rotate(x,),rotate(x,);
else
if (x==rightson[father])
rotate(father,),rotate(x,);
else rotate(x,),rotate(x,);
}
father=fa[x];
}
}
void access(int x)
{
for (int y=;x;y=x,x=fa[x])
splay(x),rightson[x]=y,updata(x);
}
void makeroot(int x)
{
access(x);
splay(x);
rev[x]^=;
}
int findroot(int x)
{
access(x);
splay(x);
while (leftson[x]) x=leftson[x];
return x;
}
void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
}
bool link(int x,int y)
{
makeroot(x);
if (findroot(y)!=x)
{
fa[x]=y;
return();
}
else return();
}
void cut(int x,int y)
{
makeroot(x);
if (findroot(y)==x&&fa[x]==y)
{
fa[x]=leftson[y]=;
updata(y);
}
}
} num[M];
struct hash{
#define mo 5000007
int cc=;
struct {
int a,b,c;
}f[mo+];
void push(int x,int y,int z)
{
int tmp=(cc*x+y)%mo;
while (f[tmp].a) tmp=tmp%mo+;
f[tmp].a=x; f[tmp].b=y; f[tmp].c=z;
}
int find(int x,int y)
{
int tmp=(cc*x+y)%mo;
while (f[tmp].a&&(!(f[tmp].a==x&&f[tmp].b==y))) tmp=tmp%mo+;
if (f[tmp].a==x&&f[tmp].b==y) return(tmp);
else return(-);
}
}hash;
int main()
{
freopen("network.in","r",stdin);
freopen("network.out","w",stdout);
cin>>n>>m>>c>>kk;
for (int i=;i<=n;i++)
cin>>v[i];
for (int i=;i<=m;i++)
{
int c,d,e;
cin>>c>>d>>e;
hash.push(c,d,e); hash.push(d,c,e);
cnt[e][c]++; cnt[e][d]++;
num[e].link(c,d);
}
int k,x,y,z;
for (int i=;i<=kk;i++)
{
cin>>k;
if (k==)
{
cin>>x>>y;
for (int i=;i<c;i++)
{
num[i].access(x);
num[i].splay(x);
v[x]=y;
num[i].updata(x);
}
}
if (k==)
{
cin>>x>>y>>z;
int tmp=hash.find(x,y),tmp2=hash.find(y,x),tmp3=hash.f[tmp].c;
if (tmp==-) cout<<"No such edge."<<endl; else
if (tmp3==z) cout<<"Success."<<endl; else
if ((cnt[z][x]==||cnt[z][y]==)) cout<<"Error 1."<<endl; else
if (num[z].link(x,y))
{
num[tmp3].cut(x,y);
hash.f[tmp].c=z; hash.f[tmp2].c=z;
cnt[tmp3][x]--; cnt[tmp3][y]--;
cnt[z][x]++; cnt[z][y]++;
cout<<"Success."<<endl;
} else cout<<"Error 2."<<endl;
}
if (k==)
{
cin>>z>>x>>y;
num[z].makeroot(x);
if (num[z].findroot(y)==x)
{
num[z].split(x,y);
cout<<num[z].data[y]<<endl;;
} else cout<<-<<endl;
}
}
return ;
}
04-30 19:10