[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1180
[算法]
动态树维护森林连通性
时间复杂度 : O(NlogN ^ 2)
[代码]
#include<bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; typedef long long ll; typedef long double ld; typedef unsigned long long ull; int n; int val[MAXN]; struct Link_Cut_Tree { struct Node { int father , son[2] , value , cnt; bool tag; } a[MAXN]; inline void init() { for (int i = 1; i <= n; i++) { a[i].value = a[i].cnt = val[i]; a[i].tag = false; a[i].father = a[i].son[0] = a[i].son[1] = 0; } } inline void update(int x) { a[x].cnt = a[a[x].son[0]].cnt + a[a[x].son[1]].cnt + a[x].value; } inline void pushdown(int x) { if (a[x].tag) { swap(a[x].son[0] , a[x].son[1]); a[a[x].son[0]].tag ^= 1; a[a[x].son[1]].tag ^= 1; a[x].tag = false; } } inline bool get(int x) { pushdown(a[x].father); return a[a[x].father].son[1] == x; } inline bool nroot(int x) { return a[a[x].father].son[0] == x | a[a[x].father].son[1] == 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 ^ 1]; if (nroot(f)) a[g].son[tmpf] = x; a[x].son[tmpx ^ 1] = 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 = 0; static int st[MAXN]; st[++z] = y; while (nroot(y)) y = st[++z] = 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[0] == y) ^ (a[y].son[0] == x) ? x : y); rotate(x); } update(x); } inline void access(int x) { for (int y = 0; x; x = a[y = x].father) { splay(x); a[x].son[1] = y; update(x); } } inline void make_root(int x) { access(x); splay(x); a[x].tag ^= 1; pushdown(x); } inline int find_root(int x) { access(x); splay(x); while (a[x].son[0]) { pushdown(x); x = a[x].son[0]; } return x; } inline void link(int x , int y) { make_root(x); if (find_root(y) != x) a[x].father = y; } inline void split(int x , int y) { make_root(x); access(y); splay(y); } inline int query(int x , int y) { split(x , y); return a[y].cnt; } inline bool connected(int x , int y) { return (find_root(x) == find_root(y)); } inline void modify(int u , int x) { splay(u); a[u].value = x; update(u); } } LCT; 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 = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } int main() { scanf("%d" , &n); for (int i = 1; i <= n; i++) scanf("%d" , &val[i]); LCT.init(); int q; scanf("%d" , &q); while (q--) { char type[10]; scanf("%s" , type); if (type[0] == 'b') { int u , v; scanf("%d%d" , &u , &v); if (LCT.connected(u , v)) { printf("no "); } else { printf("yes "); LCT.link(u , v); } } else if (type[0] == 'p') { int u , x; scanf("%d%d" , &u , &x); LCT.modify(u , x); } else { int u , v; scanf("%d%d" , &u , &v); if (!LCT.connected(u , v)) printf("impossible "); else printf("%d " , LCT.query(u , v)); } } return 0; }