2816: [ZJOI2012]网络

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

传送们

把一个点拆成c个即可

浪费时间的水题...

 //Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
const int N=2e5+;
typedef long long LL;
typedef double db;
using namespace std;
int n,m,c,q,cnt[N][];
LL v[N]; template<typename T> void read(T &x) {
char ch=getchar(); x=; T f=;
while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
if(ch=='-') f=-,ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
} map<LL,int>col;
LL get(int x,int y) { return (LL)x*+y; } int p[N],ch[N][],flip[N];
LL mx[N];
#define lc ch[x][0]
#define rc ch[x][1]
void update(int x) { mx[x]=max(max(mx[lc],mx[rc]),v[x]); } void down(int x) {
if(!flip[x]) return;
swap(lc,rc);
flip[lc]^=;
flip[rc]^=;
flip[x]^=;
} int isroot(int x) { return ch[p[x]][]!=x&&ch[p[x]][]!=x; } void rotate(int x) {
int y=p[x],z=p[y],l=(x==ch[y][]),r=l^;
if(!isroot(y)) ch[z][y==ch[z][]]=x; p[x]=z;
ch[y][l]=ch[x][r]; p[ch[x][r]]=y;
ch[x][r]=y; p[y]=x;
update(y); update(x);
} void splay(int x) {
static int g[N],top=,tp;
for(tp=x;!isroot(tp);tp=p[tp]) g[++top]=tp;
g[++top]=tp;
while(top) down(g[top--]);
for(;!isroot(x);rotate(x)) {
int y=p[x],z=p[y];
if(!isroot(y)) ((x==ch[y][])^(y==ch[z][]))?rotate(x):rotate(y);
}
} void access(int x) {
for(int t=;x;x=p[t=x]) {
splay(x);
rc=t;
update(x);
}
} int find_root(int x) {
access(x);
splay(x);
while(lc) x=lc;
return x;
} void newroot(int x) {
access(x);
splay(x);
flip[x]^=;
} void lik(int x,int y) {
newroot(x);
splay(x);
splay(y);
p[x]=y;
} void cut(int x,int y) {
newroot(x);
access(y);
splay(y);
if(ch[y][]==x) ch[y][]=p[x]=;
update(y);
} void change(int x,int y) {
splay(x);
v[x]=y;
update(x);
} void qry(int x,int y) {
if(find_root(x)!=find_root(y)) {
puts("-1"); return;
}
newroot(x);
access(y);
splay(y);
printf("%lld\n",mx[y]);
} #define DEBUG
int main() {
#ifdef DEBUG
freopen("std.in","r",stdin);
//freopen(".out","w",stdout);
#endif
read(n); read(m); read(c); read(q);
For(i,,n) {
read(v[i]);
For(j,,c-) v[i+j*n]=v[i];
}
For(i,,m) {
int u,v,w;
read(u); read(v); read(w); w++;
cnt[u][w]++; cnt[v][w]++;
col[get(u,v)]=col[get(v,u)]=w;
lik(u+(w-)*n,v+(w-)*n);
}
For(ti,,q) {
int o,x,y,u,v,nw;
read(o);
if(o==) {
read(x); LL xx; read(xx);
For(i,,c)
change(x+(i-)*n,xx);
}
else if(o==) {
read(u); read(v); read(nw); nw++;
int w=col[get(u,v)];
if(!w) { puts("No such edge."); continue; }
if(w==nw) { puts("Success."); continue; }
if(cnt[u][nw]+>||cnt[v][nw]+>) { puts("Error 1."); continue; }
if(find_root((nw-)*n+u)==find_root((nw-)*n+v)) { puts("Error 2."); continue; }
col[get(u,v)]=col[get(v,u)]=nw;
cnt[u][w]--; cnt[v][w]--;
cnt[u][nw]++; cnt[v][nw]++;
cut((w-)*n+u,(w-)*n+v);
lik((nw-)*n+u,(nw-)*n+v);
puts("Success.");
}
else {
read(nw); read(u); read(v);
qry(nw*n+u,nw*n+v);
}
}
return ;
}