zoukankan      html  css  js  c++  java
  • UOJ207:共价大爷游长沙

    题面

    UOJ

    Sol

    神题
    给每个点对随机一个权值,把这两个点的权值异或上这个随机的值
    (LCT)维护子树信息,若子树异或和为所有点对的异或和那么就是答案

    大常数代码

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(1e5 + 5);
    
    IL int Input(){
        RG int x = 0, z = 1; RG char c = getchar();
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        return x * z;
    }
    
    int n, m, ch[_][2], fa[_], size[_], sum[_], val[_], rev[_], S[_], top, Sum;
    struct Edge{
    	int u, v, w;
    } P[_ << 2];
    
    # define ls ch[x][0]
    # define rs ch[x][1]
    
    IL bool Son(RG int x){
    	return ch[fa[x]][1] == x;
    }
    
    IL bool Isroot(RG int x){
    	return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
    }
    
    IL void Reverse(RG int x){
    	if(!x) return;
    	rev[x] ^= 1; swap(ls, rs);	
    }
    
    IL void Pushdown(RG int x){
    	if(!rev[x]) return;
    	Reverse(ls); Reverse(rs); rev[x] = 0;	
    }
    
    IL void Update(RG int x){
    	sum[x] = val[x] ^ size[x] ^ sum[ls] ^ sum[rs];
    }
    
    IL void Rotate(RG int x){
    	RG int y = fa[x], z = fa[y], c = Son(x);
    	if(!Isroot(y)) ch[z][Son(y)] = x; fa[x] = z;
    	ch[y][c] = ch[x][!c]; fa[ch[y][c]] = y;
    	ch[x][!c] = y; fa[y] = x;
    	Update(y);
    }
    
    IL void Splay(RG int x){
    	S[top = 1] = x;
    	for(RG int y = x; !Isroot(y); y = fa[y]) S[++top] = fa[y];
    	while(top) Pushdown(S[top--]);
    	for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
    		if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
    	Update(x);
    }
    
    IL void Access(RG int x){
    	for(RG int y = 0; x; y = x, x = fa[x]){
    		Splay(x);
    		size[x] ^= sum[rs] ^ sum[y];
    		rs = y;	Update(x);
    	}
    }
    
    IL void Makeroot(RG int x){
    	Access(x); Splay(x); Reverse(x);
    }
    
    IL void Link(RG int x, RG int y){
    	Makeroot(x); Makeroot(y); fa[x] = y;
    	size[y] ^= sum[x]; Update(y);
    }
    
    IL void Split(RG int x, RG int y){
    	Makeroot(x); Access(y); Splay(y);
    }
    
    IL void Cut(RG int x, RG int y){
    	Split(x, y); fa[x] = ch[y][0] = 0;
    }
    
    # define yyb 141905
    # define lrh 141936
    # define ni_dong_de +
    
    int main(RG int argc, RG char* argv[]){
    	srand(yyb ni_dong_de lrh);
    	Input(); n = Input(); m = Input();
    	for(RG int i = 1, x, y; i < n; ++i)
    		x = Input(), y = Input(), Link(x, y);
    	for(RG int i = 1, tot = 0; i <= m; ++i){
    		RG int opt = Input(), x = Input(), y, u, v;
    		if(opt == 1){
    			y = Input(), u = Input(), v = Input();
    			Cut(x, y), Link(u, v);
    		}
    		else if(opt == 2){
    			P[++tot].u = x, P[tot].v = y = Input();
    			P[tot].w = rand() % 19260817;
    			Makeroot(x); val[x] ^= P[tot].w; Update(x);
    			Makeroot(y); val[y] ^= P[tot].w; Update(y);
    			Sum ^= P[tot].w;
    		}
    		else if(opt == 3){
    			Makeroot(P[x].u); val[P[x].u] ^= P[x].w; Update(P[x].u);
    			Makeroot(P[x].v); val[P[x].v] ^= P[x].w; Update(P[x].v);
    			Sum ^= P[x].w;
    		}
    		else{
    			y = Input(); Split(x, y);
    			if(sum[x] ^ Sum) puts("NO");
    			else puts("YES");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    Android Studio 或 IntelliJ IDEA获取数字签名的方法
    android四大组件学习总结以及各个组件示例(2)
    android四大组件学习总结以及各个组件示例(1)
    Android利用canvas画画板
    Android service 服务的应用之电话监听器以及短信监听器
    Android Gesture 手势创建以及使用示例
    安卓http源码查看器详解
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    二叉树的非递归遍历(栈)
    python 游戏(滑动拼图Slide_Puzzle)
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8427481.html
Copyright © 2011-2022 走看看