[ZJOI 2012] 网络

时间:2023-03-10 04:41:26
[ZJOI 2012] 网络

[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=2816

[算法]

对每种颜色的边建一棵LCT , 维护联通性即可

时间复杂度 : O(C * NlogN ^ 2)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
const int MAXC = ;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull; int n , m , c , k;
int u[MAXN] , v[MAXN] , w[MAXN] , val[MAXN] , cnt[MAXC][MAXN];
map< pair<int , int> , int> mp; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} struct Link_Cut_Tree
{
struct Node
{
int father , son[] , value , mx;
bool tag;
} a[MAXN];
inline void init()
{
for (int i = ; i <= n; i++)
{
a[i].father = ;
a[i].son[] = a[i].son[] = ;
a[i].value = a[i].mx = val[i];
a[i].tag = false;
}
}
inline void pushdown(int x)
{
if (a[x].tag)
{
swap(a[x].son[] , a[x].son[]);
a[a[x].son[]].tag ^= ;
a[a[x].son[]].tag ^= ;
a[x].tag = false;
}
}
inline void update(int x)
{
a[x].mx = a[x].value;
if (a[x].son[]) chkmax(a[x].mx , a[a[x].son[]].mx);
if (a[x].son[]) chkmax(a[x].mx , a[a[x].son[]].mx);
}
inline bool get(int x)
{
pushdown(x);
return a[a[x].father].son[] == x;
}
inline bool nroot(int x)
{
return a[a[x].father].son[] == x || a[a[x].father].son[] == x;
}
inline void rotate(int x)
{
int f = a[x].father , g = a[f].father;
int tmpx = get(x) , tmpf = get(f);
int w = a[x].son[tmpx ^ ];
if (nroot(f)) a[g].son[tmpf] = x;
a[x].son[tmpx ^ ] = f;
a[f].son[tmpx] = w;
if (w) a[w].father = f;
a[f].father = x;
a[x].father = g;
update(f);
}
inline void splay(int x)
{
int y = x , z = ;
static int st[MAXN];
st[++z] = y;
while (nroot(y)) st[++z] = y = a[y].father;
while (z) pushdown(st[z--]);
while (nroot(x))
{
int y = a[x].father , z = a[y].father;
if (nroot(y))
rotate((a[z].son[] == y) ^ (a[y].son[] == x) ? x : y);
rotate(x);
}
update(x);
}
inline void access(int x)
{
for (int y = ; x; x = a[y = x].father)
{
splay(x);
a[x].son[] = y;
update(x);
}
}
inline void make_root(int x)
{
access(x);
splay(x);
a[x].tag ^= ;
pushdown(x);
}
inline void link(int x , int y)
{
make_root(x);
if (find_root(y) != x) a[x].father = y;
}
inline int find_root(int x)
{
access(x);
splay(x);
while (a[x].son[])
{
pushdown(x);
x = a[x].son[];
}
return x;
}
inline void cut(int x , int y)
{
make_root(x);
if (find_root(y) == x && a[x].father == y && !a[x].son[])
{
a[x].father = a[y].son[] = ;
update(y);
}
}
inline void split(int x , int y)
{
make_root(x);
access(y);
splay(y);
}
inline void modify(int x , int y)
{
splay(x);
a[x].value = y;
update(x);
}
inline bool connected(int x , int y)
{
return (find_root(x) == find_root(y));
}
inline int query(int x , int y)
{
split(x , y);
return a[y].mx;
}
} T[MAXC]; int main()
{ read(n); read(m); read(c); read(k);
for (int i = ; i <= n; i++) read(val[i]);
for (int i = ; i < c; i++) T[i].init();
for (int i = ; i <= m; i++)
{
read(u[i]); read(v[i]); read(w[i]);
mp[make_pair(u[i] , v[i])] = mp[make_pair(v[i] , u[i])] = w[i];
T[w[i]].link(u[i] , v[i]);
++cnt[w[i]][u[i]]; ++cnt[w[i]][v[i]];
}
for (int i = ; i <= k; i++)
{
int type;
read(type);
if (type == )
{
int x , y;
read(x); read(y);
for (int i = ; i < c; i++) T[i].modify(x , y);
} else if (type == )
{
int u , v , w;
read(u); read(v); read(w);
if (!mp.count(make_pair(u , v)))
{
printf("No such edge.\n");
continue;
} else
{
int value = mp[make_pair(u , v)];
if (value == w)
{
printf("Success.\n");
continue;
}
if (cnt[w][u] >= || cnt[w][v] >= )
{
printf("Error 1.\n");
continue;
}
if (T[w].connected(u , v))
{
printf("Error 2.\n");
continue;
}
printf("Success.\n");
T[value].cut(u , v);
--cnt[value][u]; --cnt[value][v];
T[w].link(u , v);
mp[make_pair(u , v)] = mp[make_pair(v , u)] = w;
++cnt[w][u]; ++cnt[w][v];
}
} else
{
int w , u , v;
read(w); read(u); read(v);
if (!T[w].connected(u , v)) printf("-1\n");
else printf("%d\n" , T[w].query(u , v));
}
} return ; }