考虑对于每个子树从下往上依次考虑

对于叶子节点而言,如果可以染色,那么其\(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树合并的更多相关文章

  1. 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 ...

  2. CF888G Xor-MST 生成树、分治、Trie树合并

    传送门 第一次接触到Boruvka求最小生成树 它的原版本是:初始每一个点构成一个连通块,每一次找到每一个连通块到其他的连通块权值最短的边,然后合并这两个连通块.因为每一次连通块个数至少减半,所以复杂 ...

  3. 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 ...

  4. [杂题]:staGame(博弈论+Trie树+DFS)

    题目描述 $pure$和$dirty$决定玩$T$局游戏.对于每一局游戏,有$n$个字符串,并且每一局游戏由$K$轮组成.具体规则如下:在每一轮游戏中,最开始有一个空串,两者轮流向串的末尾添加一个字符 ...

  5. Trie 树进阶学习笔记

    前言 没脑子选手发现自己什么都不会 ... \(\text{More and more vegetables, What should I do?}\) 正文 Trie 树简介 大概是人类的话都知道吧 ...

  6. 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 ...

  7. SPOJ COT3.Combat on a tree(博弈论 Trie合并)

    题目链接 \(Description\) 给定一棵\(n\)个点的树,每个点是黑色或白色.两个人轮流操作,每次可以选一个白色的点,将它到根节点路径上的所有点染黑.不能操作的人输,求先手是否能赢.如果能 ...

  8. LOJ 2991 「THUSC 2016」补退选——trie+线段树合并或vector

    题目:https://loj.ac/problem/2291 想了线段树合并的做法.就是用线段树维护 trie 的每个点在各种时间的操作. 然后线段树合并一番,线段树维护前缀最大值,就是维护最大子段和 ...

  9. CodeForces - 778C: Peterson Polyglot (启发式合并trie树)

    Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set ...

随机推荐

  1. jeecms常用的标签

    友情链接 <dt>友情链接:</dt> [@cms_friendlink_list] [#list tag_list as link] <dd><a href ...

  2. python 喜马拉雅 音乐下载 演示代码

    1.主程序文件 import os import json import requests from contextlib import closing from progressbar import ...

  3. jQuery1.11源码分析(3)-----Sizzle源码中的浏览器兼容性检测和处理[原创]

    上一章讲了正则表达式,这一章继续我们的前菜,浏览器兼容性处理. 先介绍一个简单的沙盒测试函数. /** * Support testing using an element * @param {Fun ...

  4. expect 交互 telnet 交互

    telnet 交互 #!/bin/bash Ip="10.0.1.53" a="\{\'method\'\:\'doLogin\'\,\'params\'\:\{\'uN ...

  5. python字典转datafarm,pandas

    # coding:utf-8 import json import pandas as pd with open("./article_file/all_article.json" ...

  6. insert into与insert ignore以及replace into的区别

    insert ignore表示,如果表中已经存在相同的记录,则忽略当前新数据: INSERT INTO有无数据都插入,如果主键则不插入; REPLACE INTO 如果是主键插入则会替换以前的数据; ...

  7. word技巧

    1.插入注解(脚注和尾注) 2.复制的图片显示不全怎么办? 横向显示,或者图片另存为然后保存为PPT 3.word修订标记的添加和删除(最终版) 4.word中表格样式调整 5.修改标题的样式和标题的 ...

  8. 关于sru源码class Model的parameters

    class Model(nn.Module): def __init__(self, words, args): super(Model, self).__init__() self.args = a ...

  9. Oracle SQL部分练习题

    SQL练习题        注:查询列表不建议用 “*” 1.列出至少有一个雇员的所有部门: a. select * from dept where deptno in(select distinct ...

  10. springcloud Zuul中路由配置细节

    上篇文章我们介绍了API网关的基本构建方式以及请求过滤,小伙伴们对Zuul的作用应该已经有了一个基本的认识,但是对于路由的配置我们只是做了一个简单的介绍,本文我们就来看看路由配置的其他一些细节. 首先 ...