SPOJ11414 COT3 博弈论 + Trie树合并

考虑对于每个子树从下往上依次考虑
对于叶子节点而言,如果可以染色,那么其\(sg\)值为\(1\),否则为\(0\)
考虑往上合并
如果选择了\(x\),那么后继状态就是其所有子树

如果选了其他子树中的一点,那么后继状态的构成如图所示

也就是,到当前根为止的所有其他子树的\(sg\)值异或上本身
那么,我们可以考虑维护一个数据结构,每次往上的时候,对于一棵子树内的点,异或上其他子树的\(sg\)值
至于查\(sg\)值,可以用一个支持查\(mex\)的东西
还需要合并
\(Trie\)树是一个不错的选择
输出答案就随意\(dfs\)一下,思路和上面的差不多
复杂度\(O(n \log n)\)
#include <map>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --)
const int sid = 2e5 + 5;
const int cid = 2e7 + 5;
#define gc getchar
inline int read() {
int p = 0, w = 1; char c = gc();
while(c > '9' || c < '0') { if(c == '-') w = -1; c = gc(); }
while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();
return p * w;
}
bool cov[cid];
int n, id, tot, cnp;
int q[sid], sg[sid], ls[cid], rs[cid], xr[cid];
int rt[sid], col[sid], cap[sid], nxt[sid], node[sid];
inline void addedge(int u, int v) {
nxt[++ cnp] = cap[u]; cap[u] = cnp; node[cnp] = v;
}
inline void put_xor(int &o, int val, int dep) {
if(dep <= -1) return;
if(val & (1 << dep)) swap(ls[o], rs[o]);
xr[o] ^= val;
}
inline void pushdown(int o, int dep) {
if(!xr[o] || !o) return;
put_xor(ls[o], xr[o], dep - 1);
put_xor(rs[o], xr[o], dep - 1);
xr[o] = 0;
}
inline void insert(int &o, int val, int dep) {
if(!o) o = ++ id;
if(dep == -1) { cov[o] = 1; return; }
if(val & (1 << dep)) insert(rs[o], val, dep - 1);
else insert(ls[o], val, dep - 1);
}
inline int merge(int x, int y, int dep) {
if(!x || !y) return x + y;
if(dep == -1) { cov[x] |= cov[y]; return x; }
pushdown(x, dep); pushdown(y, dep);
ls[x] = merge(ls[x], ls[y], dep - 1);
rs[x] = merge(rs[x], rs[y], dep - 1);
cov[x] = cov[ls[x]] && cov[rs[x]];
return x;
}
inline int mex(int o, int dep) {
if(!o || dep == -1) return 0;
pushdown(o, dep);
if(!cov[ls[o]]) return mex(ls[o], dep - 1);
else return (1 << dep) + mex(rs[o], dep - 1);
}
#define cur node[i]
inline void dfs(int o, int fa) {
int nsg = 0;
for(int i = cap[o]; i; i = nxt[i])
if(cur != fa) dfs(cur, o), nsg ^= sg[cur];
if(!col[o]) insert(rt[o], nsg, 17);
for(int i = cap[o]; i; i = nxt[i])
if(cur != fa) {
put_xor(rt[cur], nsg ^ sg[cur], 17);
rt[o] = merge(rt[o], rt[cur], 17);
}
sg[o] = mex(rt[o], 17);
}
inline void find(int o, int fa, int SG) {
for(int i = cap[o]; i; i = nxt[i])
if(cur != fa) SG ^= sg[cur];
if(SG == 0 && !col[o]) q[++ tot] = o;
for(int i = cap[o]; i; i = nxt[i])
if(cur != fa) find(cur, o, SG ^ sg[cur]);
}
int main() {
n = read();
rep(i, 1, n) col[i] = read();
rep(i, 2, n) {
int u = read(), v = read();
addedge(u, v); addedge(v, u);
}
dfs(1, 0);
find(1, 0, 0);
if(tot) {
sort(q + 1, q + tot + 1);
rep(i, 1, tot) printf("%d\n", q[i]);
}
else puts("-1");
return 0;
}
SPOJ11414 COT3 博弈论 + Trie树合并的更多相关文章
- Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并
D. Acyclic Organic Compounds You are given a tree T with n vertices (numbered 1 through n) and a l ...
- CF888G Xor-MST 生成树、分治、Trie树合并
传送门 第一次接触到Boruvka求最小生成树 它的原版本是:初始每一个点构成一个连通块,每一次找到每一个连通块到其他的连通块权值最短的边,然后合并这两个连通块.因为每一次连通块个数至少减半,所以复杂 ...
- 2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- [杂题]:staGame(博弈论+Trie树+DFS)
题目描述 $pure$和$dirty$决定玩$T$局游戏.对于每一局游戏,有$n$个字符串,并且每一局游戏由$K$轮组成.具体规则如下:在每一轮游戏中,最开始有一个空串,两者轮流向串的末尾添加一个字符 ...
- Trie 树进阶学习笔记
前言 没脑子选手发现自己什么都不会 ... \(\text{More and more vegetables, What should I do?}\) 正文 Trie 树简介 大概是人类的话都知道吧 ...
- SPOJ COT3 Combat on a tree(Trie树、线段树的合并)
题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...
- SPOJ COT3.Combat on a tree(博弈论 Trie合并)
题目链接 \(Description\) 给定一棵\(n\)个点的树,每个点是黑色或白色.两个人轮流操作,每次可以选一个白色的点,将它到根节点路径上的所有点染黑.不能操作的人输,求先手是否能赢.如果能 ...
- LOJ 2991 「THUSC 2016」补退选——trie+线段树合并或vector
题目:https://loj.ac/problem/2291 想了线段树合并的做法.就是用线段树维护 trie 的每个点在各种时间的操作. 然后线段树合并一番,线段树维护前缀最大值,就是维护最大子段和 ...
- CodeForces - 778C: Peterson Polyglot (启发式合并trie树)
Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set ...
随机推荐
- idea2017破解方法
破解方法 切换license server然后 输入网址:http://idea.iteblog.com/key.php即可
- vue element-ui表格里时间戳转换成时间显示
工作中遇到后台给的表格数据里时间是一个13位的时间戳,需要转换成时间显示在表格里, 可以用element-ui表格自带的:formatter函数,来格式化表格内容: // 时间戳转换成时间 // 使用 ...
- [转载]TypeScript 入门指南
之前有听过,但未使用过,而最近在用nodejs,angularjs做一些前端项目,想到了这个来,正是学习TypeScript的时候,看介绍貌似和coffeescript相似,也JavaScript的转 ...
- gdb初步窥探
本文是通过学习左耳朵皓帝的文章,详见:http://blog.csdn.net/haoel 1.使用gdb gdb主要是用来调试c和c++程序,首先在编译前我们先把调试信息加到可执行程序当中,使用参数 ...
- expect 交互 模拟ssh 登陆
模拟ssh登录 #!/bin/bash Ip='192.168.1.6' # 循环就行 RemoteUser='user' # 普通用户 RemotePasswd='userpasswd' # 普通用 ...
- 第11月第11天 avplayer循环播放
1. /* Setting actionAtItemEnd to None prevents the movie from getting paused at item end. A very sim ...
- Informatica学习:3、用户创建与权限管理
环境:win7 下安装Informatica 9.6.1 服务器端与客户端作为学习之用,Linux大同小异 一.用户创建(服务器端) 1.登陆admin console (1)打开Admin Cons ...
- vue实战之狗血事件:页面loading效果诡异之事
接上回 想加一个切换路由时,跳出一个loading动画 ,路由加载后就消失 先做了一个loading提示的浮动层的组件,全局注册,在几个路由页面都引入 在vuex里面维护一个变量比如isLoading ...
- python psutil监控系统资源【转】
通过 运用 Python 第三方 系统 基础 模块, 可以 轻松 获取 服务 关键 运营 指标 数据,包括 Linux 基本 性能. 块 设备. 网卡 接口. 系统 信息. 网络 地址 库 等 信息. ...
- c# XML读取
System.Xml 命名空间 https://msdn.microsoft.com/zh-cn/library/gg145036(v=vs.110).aspx 一.读取 1.通过 XmlDocume ...