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 ...
随机推荐
- python---django使用数据库(orm)
官方教程点击此处 项目默认使用sqlite DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os. ...
- bzoj千题计划208:bzoj3174: [Tjoi2013]拯救小矮人
http://www.lydsy.com/JudgeOnline/problem.php?id=3174 按a+b从小到大排序,a+b小的在上面,先考虑让它逃出去 正确性不会证 感性理解一下,最后一个 ...
- IOS 与 PHP 通信加密,使用AES 128 CBC no padding
这个网上的资料真实浩如烟海,但是真正有价值的屈指可数 自己尝试了一天多,终于还是搞定了. 再次要感谢网上的前辈么. 比如下面这个关于php和java端的实现: http://my.oschina.ne ...
- ubuntu 开机自动挂载分区
转载: http://blog.sina.com.cn/s/blog_142e95b170102vx2a.html 我的计算机是双硬盘,一个是windows系统,一个是Fedora和ubuntu系统. ...
- Palindrome Partitioning I & II
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【转】深入理解C++中public、protected及private用法
首先明白以下两点: 1.类的一个特征就是封装,public和private作用就是实现这一目的. 即:用户代码(类外)可以访问public成员而不能访问private成员:private成员只能由类成 ...
- 003_vim使用tip
vim 使用tip 编写python程序 自动插入头信息: #!/usr/bin/env python # coding=utf-8 输入.或按TAB键会触发代码补全功能 :w保存代码之后会自动检查代 ...
- 启动虚拟机报错VMware Workstation cannot connect to the virtual machine
启动虚拟机报错: VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run ...
- Maven打包Swing程序
有两个maven工程:见下. 第二个工程依赖第一个工程,第二个工程是swing,两个工程都需要链接数据库. 打包步骤: 1.主要第二个工程主类,run as --->java applicati ...
- 解决cef中title不现实tooltip的问题
本文转自:https://blog.csdn.net/hu1340748/article/details/79030569 感谢感谢 最近在使用chromiumFX做项目,突然发现页面标签中的titl ...