• 【UOJ #266】【清华集训2016】Alice和Bob又在玩游戏(SG函数+01Trie)


    传送门

    Combat on a treeCombat on a tree没有区别
    选了一条链之后各个子树互相独立
    对于一个点子树内所有点都可以选

    可以用01Trie01Trie维护选每个子孙的SGSG函数值
    往上可以直接01Trie01Trie合并,然后打上其他子树的异或和的标记
    每次查询mexmex即可

    #include<bits/stdc++.h>
    using namespace std;
    #define cs const
    #define pb push_back
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define ll long long
    cs int RLEN=1<<20|1;
    inline char gc(){
    	static char ibuf[RLEN],*ib,*ob;
    	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    	return (ib==ob)?EOF:*ib++;
    }
    inline int read(){
    	char ch=gc();
    	int res=0;bool f=1;
    	while(!isdigit(ch))f^=ch=='-',ch=gc();
    	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    	return f?res:-res;
    }
    cs int N=100005;
    namespace Trie{
    	cs int M=N*18;
    	int vt[M],son[M][2],tag[M],tot;
    	#define lc(u) son[u][0]
    	#define rc(u) son[u][1]
    	inline void clear(){
    		while(tot)lc(tot)=rc(tot)=0,tag[tot]=0,vt[tot--]=0;
    	}
    	inline void pushnow(int u,int k,int dep){
    		if(k&(1<<dep))swap(lc(u),rc(u));
    		tag[u]^=k;
    	}
    	inline void pushdown(int u,int dep){
    		if(!tag[u]||dep==0)return;
    		if(lc(u))pushnow(lc(u),tag[u],dep-1);
    		if(rc(u))pushnow(rc(u),tag[u],dep-1);
    		tag[u]=0;
    	}
    	void merge(int &u,int r1,int r2,int dep){
    		if(!r1||!r2){u=r1+r2;return;}
    		pushdown(r1,dep),pushdown(r2,dep);
    		u=r1,vt[u]=vt[r1]|vt[r2];
    		merge(lc(u),lc(r1),lc(r2),dep-1),merge(rc(u),rc(r1),rc(r2),dep-1);
    		vt[u]|=vt[lc(u)]&vt[rc(u)];
    	}
    	void insert(int &u,int k,int dep){
    		if(!u)u=++tot;
    		if(dep==-1){vt[u]=1;return;}		
    		pushdown(u,dep);
    		if(k&(1<<dep))insert(rc(u),k,dep-1);
    		else insert(lc(u),k,dep-1);
    		vt[u]=vt[lc(u)]&vt[rc(u)];
    	}
    	int mex(int u,int dep){
    		if(dep<0)return 0;
    		pushdown(u,dep);
    		if(vt[lc(u)])return (1<<dep)+mex(rc(u),dep-1);
    		else return mex(lc(u),dep-1);
    	}
    }
    vector<int> e[N];
    int n,m;
    int vis[N],rt[N],sg[N];
    void dfs(int u,int fa){
    	int s=0;vis[u]=1;
    	for(int &v:e[u]){
    		if(v==fa)continue;
    		dfs(v,u);
    		s^=sg[v];
    	}
    	Trie::insert(rt[u],s,16);
    	for(int &v:e[u]){
    		if(v==fa)continue;
    		Trie::pushnow(rt[v],s^sg[v],16);
    		Trie::merge(rt[u],rt[u],rt[v],16);
    	}
    	sg[u]=Trie::mex(rt[u],16);
    }
    inline void solve(){
    	n=read(),m=read();
    	for(int i=1;i<=m;i++){
    		int u=read(),v=read();
    		e[u].pb(v),e[v].pb(u);
    	}
    	int ret=0;
    	for(int i=1;i<=n;i++)if(!vis[i])dfs(i,0),ret^=sg[i];
    	if(ret>0)puts("Alice");
    	else puts("Bob");
    	for(int i=1;i<=n;i++)e[i].clear(),rt[i]=vis[i]=sg[i]=0;
    	Trie::clear();
    }
    int main(){
    	#ifdef Stargazer
    	freopen("lx.in","r",stdin);
    	#endif
    	int T=read();
    	while(T--)solve();
    }
    
    
  • 相关阅读:
    蛤圈不能圈一切---逐梦蛤蛤圈
    Unix培训基础
    java培训基础
    数据库基础
    Python生成requirements.txt方法
    免费高质量代理IP
    lxml库的xpath的使用
    python自带GUI库tkinter教程,全网最全最好
    selenium博客
    Linux-Centos下selenium部署
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328354.html
Copyright © 2020-2023  润新知