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 ...
随机推荐
- Hadoop生态圈-Kafka的旧API实现生产者-消费者
Hadoop生态圈-Kafka的旧API实现生产者-消费者 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.旧API实现生产者-消费者 1>.开启kafka集群 [yinz ...
- 在angularJs实现批量删除
原理:在js中定义一个数组,然后给每个复选框一个点击事件,点击事件的方法参数有两个,一个是事件源$event,一个是id.点击复选框根据事件源判断是否被选中,然后进而是向这个数组增加或者删除id. $ ...
- js 正则学习小记之匹配字符串字面量优化篇
昨天在<js 正则学习小记之匹配字符串字面量>谈到 个字符,除了第一个 个,只有 个转义( 个字符),所以 次,只有 次成功.这 次匹配失败,需要回溯后用 [^"] 才能匹配成功 ...
- [原] eclipse 无法找到 run as junit
碰见这个问题,折磨我好一下! 问题根源和解决方式 第一,保证有junit jar包,基本不会犯这错误: 第二,保证你这个类是Source可编译文件,要是这个类在普通文件夹下,工程是不会编译它的,也就找 ...
- SVM Kernel Functions
==================================================================== This article came from here. Th ...
- Javascript - Vue - 指令
指令 v-cloak 解决闪烁,闪烁是指在网速较慢的情况下可能会出现插值表达式{{}}还没有填充数据时会把该表达式直接显示在页面上,如果不希望看到插值表达式则可以使用v-cloak指令,具体做法如下 ...
- 安装informatic过程中的错误
1.Check if the DISPLAY variable is set export DISPLAY=192.168.3.201:0.0 在注销用户并切换到oracle或者infa 用户,就可以 ...
- VCForPython27.msi安装后, 还显示error: Unable to find vcvarsall.bat
C:\Users\zpc\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC 增加环境变量: SET VCPYTH ...
- MySQL管理工具MySQL Utilities — 介绍与安装(1)
MySQL Utilities介绍 MySQL Utilities 提供一组命令行工具用于维护和管理 MySQL 服务器,包括: 管理工具 (克隆.复制.比较.差异.导出.导入) 复制工具 (安装.配 ...
- WPF 中定时器的使用
DispatcherTimer timer; private void Window_Loaded(object sender, RoutedEventArgs e) { timer = new Di ...