「UOJ207」共价大爷游长沙

「UOJ207」共价大爷游长沙

解题思路 :

快速判断两个集合是否完全相等可以随机点权 \(\text{xor}\) 的思路可以用到这道题上面,给每一条路径随机一个点权,维护出经过每一条边的点权的 \(\text{xor}\) 值判断是否和全集相等即可。

因为要支持删边加边操作,可以用一棵 \(\text{lct}\) 来维护。对于删边,相当于是原来经过这条边的路径要改为从新的树上的那条路径经过,那只要将原有的 \(\text{xor}\) 值修改过去即可。

/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf ((int)(1e9))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
    int f = 0, ch = 0; x = 0;
    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
    for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    if(f) x = -x;
}

const int N = 1000005;

map<int, int> mp[N];
int n, m, cnt, id;
struct Node{ int a, b, c; } dd[N];

namespace LCT{
	int ch[N][2], val[N], rev[N], tag[N], fa[N], s[N];
	#define P(u) (ch[fa[u]][1] == u)
	#define IS(u) (ch[fa[u]][0] != u && ch[fa[u]][1] != u)
	inline void update(int u){ 
		s[u] = val[u] ^ s[ch[u][0]] ^ s[ch[u][1]]; 
	}
	inline void pushdown(int u){
		if(rev[u]){	
			rev[u] = 0, swap(ch[u][0], ch[u][1]);
			if(ch[u][0]) rev[ch[u][0]] ^= 1;
			if(ch[u][1]) rev[ch[u][1]] ^= 1;
		}
		if(tag[u]){
			val[u] ^= tag[u];
			if(ch[u][0]) tag[ch[u][0]] ^= tag[u];
			if(ch[u][1]) tag[ch[u][1]] ^= tag[u];
			tag[u] = 0;
		}
	}
	inline void rotate(int u){
		int F = fa[u], w = P(u), G = fa[F];
		if(!IS(F)) ch[G][P(F)] = u; fa[u] = G;
		ch[F][w] = ch[u][w^1], fa[ch[u][w^1]] = F;
		ch[u][w^1] = F, fa[F] = u, update(F);
	}
	inline void splay(int u){
		static int st[N], top; st[top=1] = u;
		for(int i = u; !IS(i); i = fa[i]) st[++top] = fa[i];
		for(int i = top; i; i--) pushdown(st[i]);
		for(int F = fa[u]; !IS(u); rotate(u), F = fa[u])
			if(!IS(F)) rotate(P(u) == P(F) ? F : u);
		update(u);
	}
	inline void access(int u){
		for(int c = 0; u; c = u, u = fa[u]) 
			splay(u), ch[u][1] = c, update(u);
	}
	inline void makeroot(int u){
		access(u), splay(u), rev[u] ^= 1;
	}
	inline void link(int x, int y){
		makeroot(x), fa[x] = y;
	}
	inline void cut(int x, int y){
		makeroot(x), access(y), splay(y);
		ch[y][0] = fa[x] = 0;
	}
	inline int query(int x){
		makeroot(x), access(x), splay(x);
		return s[x];
	}
	inline void change(int x, int y, int z){
		makeroot(x), access(y), splay(y);
		tag[y] ^= z, pushdown(y);
	}
}

int main(){
	read(id), read(n), read(m), id = n;
	for(int i = 1, x, y; i < n; i++){
		read(x), read(y), ++id;
		LCT::link(x, id), LCT::link(y, id);
		mp[x][y] = mp[y][x] = id;
	}
	int XOR = 0; 
	for(int i = 1, op, a, b, c, d; i <= m; i++){
		read(op);
		if(op == 1){
			read(a), read(b), read(c), read(d);
			int e = mp[a][b], tmp = LCT::query(e);
			LCT::cut(a, e), LCT::cut(b, e), e = ++id;
			mp[a][b] = mp[b][a] = 0;
			mp[c][d] = mp[d][c] = e;
			LCT::link(c, e), LCT::link(d, e);
			LCT::change(a, b, tmp);
		}
		if(op == 2){
			read(a), read(b); 
			int tmp = 1ll * rand() * rand() % ((int)1e9);
			LCT::change(a, b, tmp), XOR ^= tmp;
			dd[++cnt] = (Node){ a, b, tmp };
		}
		if(op == 3){
			read(d); 
			a = dd[d].a, b = dd[d].b, c = dd[d].c;
			LCT::change(a, b, c), XOR ^= c;
		}
		if(op == 4){
			read(a), read(b);
			puts(LCT::query(mp[a][b]) == XOR ? "YES" : "NO");		
		}
	}
	return 0;
}
posted @ 2018-12-21 16:49  Joyemang33  阅读(246)  评论(0编辑  收藏  举报