您的位置:首页 > 理论基础 > 计算机网络

[bzoj2816][ZJOI2012]网络(LCT,splay)

2018-08-05 14:28 309 查看
传送门

[b]题解[/b]

话说以前还真没见过用LCT只维护一条链的……好像除了树点涂色那题……

先看一下题目规定的两个性质

对于任意节点连出去的边中,相同颜色的边不超过两条。

图中不存在同色的环,同色的环指相同颜色的边构成的环。

很明显了,同一种颜色肯定是由几条链组成的(虽然我根本没有发现)

然后又要查询边权和维护路径……直接上LCT吧

然后颜色数很少啊……每一个颜色开一个LCT好了

更改权值的话在每一个LCT上splay一下

修改颜色的话在原来的LCT中cut,新的LCT中link

查询路径直接split出来

然后差不多就这样了

//minamoto
#include<cstdio>
#include<map>
#include<iostream>
using std::swap;
using std::map;
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){
#define num ch-'0'
char ch;bool flag=0;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*10+num);
(flag)&&(res=-res);
#undef num
return res;
}
const int N=10005;
int n,m,val
,c,k;
struct link_cut_tree{
int fa
,ch
[2],rev
,mx
,cnt
,s
,top;
inline bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
inline void pushup(int x){
mx[x]=val[x];int l=ch[x][0],r=ch[x][1];
if(l) cmax(mx[x],mx[l]);
if(r) cmax(mx[x],mx[r]);
}
inline void pushdown(int x){
if(x&&rev[x]){
swap(ch[x][0],ch[x][1]);
rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
rev[x]=0;
}
}
void rotate(int x){
int y=fa[x],z=fa[y],d=ch[y][1]==x;
if(!isroot(y)) ch[z][ch[z][1]==y]=x;
fa[x]=z,fa[y]=x,fa[ch[x][d^1]]=y,ch[y][d]=ch[x][d^1],ch[x][d^1]=y,pushup(y);
}
void splay(int x){
s[top=1]=x;for(int i=x;!isroot(i);i=fa[i]) s[++top]=fa[i];
while(top) pushdown(s[top--]);
for(int y=fa[x],z=fa[y];!isroot(x);y=fa[x],z=fa[y]){
if(!isroot(y))
((ch[y][1]==x)^(ch[z][1]==y))?rotate(x):rotate(y);
rotate(x);
}
pushup(x);
}
void access(int x){
for(int y=0;x;x=fa[y=x])
splay(x),ch[x][1]=y,pushup(x);
}
void makeroot(int x){
access(x),splay(x),rev[x]^=1;
}
void split(int x,int y){
makeroot(x),access(y),splay(y);
}
inline void link(int x,int y){
++cnt[x],++cnt[y],makeroot(x),fa[x]=y,splay(x);
}
inline void cut(int x,int y){
--cnt[x],--cnt[y],split(x,y),fa[x]=ch[y][0]=0,pushup(y);
}
int findroot(int x){
access(x),splay(x),pushdown(x);
while(ch[x][0]) pushdown(x=ch[x][0]);
return x;
}
inline int query(int x,int y){
split(x,y);return mx[y];
}
}lct[15];
struct edge{
int u,v;
inline bool operator <(const edge &b)const
{return u<b.u||(u==b.u&&v<b.v);}
};
map<edge,int> mp;
int main(){
//freopen("testdata.in","r",stdin);
n=read(),m=read(),c=read(),k=read();
for(int i=1;i<=n;++i) val[i]=read();
for(int i=1;i<=m;++i){
int u=read(),v=read(),w=read();
edge e1=(edge){u,v},e2=(edge){v,u};
mp[e1]=mp[e2]=w;
lct[w].link(u,v);
}
while(k--){
int opt=read();
switch(opt){
case 0:{
int x=read(),w=read();
val[x]=w;
for(int i=0;i<c;++i) lct[i].splay(x);
break;
}
case 1:{
int u=read(),v=read(),w=read();
edge a=(edge){u,v},b=(edge){v,u};
if(!mp.count(a)){puts("No such edge.");continue;}
int x=mp[a];
if(x==w){puts("Success.");continue;}
if(lct[w].cnt[u]>=2||lct[w].cnt[v]>=2){puts("Error 1.");continue;}
if(lct[w].findroot(u)==lct[w].findroot(v)){puts("Error 2.");continue;}
puts("Success.");
lct[x].cut(u,v),lct[w].link(u,v);
mp[a]=mp[b]=w;
break;
}
case 2:{
int w=read(),u=read(),v=read();
if(lct[w].findroot(u)!=lct[w].findroot(v)){puts("-1");continue;}
printf("%d\n",lct[w].query(u,v));
break;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: