SPOJ COT3 - Combat on a tree
/*
考虑直接使用暴力来算的话 SG[i]表示以i为根的子树的SG值, 然后考虑枚举删除那个子树节点, 然后求拆成的树的sg异或值, 求mex即可 复杂度三次方
然后考虑尝试 整体来做 发现对于每次子树的合并, 每棵子树种的sg值相当是异或了其他的所有子树
而这个东西显然是可以用 线段树合并来维护的
最后找出所有sg不为0的点即可
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#define ll long long
#define M 100010
#define mmp make_pair
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int cor[M], cov[M * 60], laz[M * 60], ls[M * 60], rs[M * 60], rt[M], n, sg[M], cnt, ans[M], tot;
vector<int> to[M];
void add(int &now, int v, int deep) {
if(deep <= -1) return;
if(v & (1 << deep)) swap(ls[now], rs[now]);
laz[now] ^= v;
}
void pushdown(int now, int deep) {
if(!laz[now] || !now) return;
add(ls[now], laz[now], deep - 1);
add(rs[now], laz[now], deep - 1);
laz[now] = 0;
}
int merge(int now, int last, int deep) {
if(!now || !last) return now | last;
if(deep == -1) {
cov[now] |= cov[last];
return now;
}
pushdown(now, deep), pushdown(last, deep);
ls[now] = merge(ls[now], ls[last], deep - 1);
rs[now] = merge(rs[now], rs[last], deep - 1);
cov[now] = cov[ls[now]] && cov[rs[now]];
return now;
}
void insert(int &now, int v, int deep) {
if(!now) now = ++cnt;
if(deep == -1) {
cov[now] = 1;
return;
}
if(v & (1 << deep)) insert(rs[now], v, deep - 1);
else insert(ls[now], v, deep - 1);
}
int mex(int now, int deep) {
if(!now || deep == -1) return 0;
pushdown(now, deep);
if(!cov[ls[now]]) return mex(ls[now], deep - 1);
else return (1 << deep) + mex(rs[now], deep - 1);
}
void dfs(int now, int fa) {
int tmp = 0;
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == fa) continue;
dfs(vj, now);
tmp ^= sg[vj];
}
if(!cor[now]) insert(rt[now], tmp, 17);
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == fa) continue;
add(rt[vj], tmp ^ sg[vj], 17);
rt[now] = merge(rt[now], rt[vj], 17);
}
sg[now] = mex(rt[now], 17);
}
void get(int now, int fa, int anss) {
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == fa) continue;
anss ^= sg[vj];
}
if(anss == 0 && !cor[now]) ans[++tot] = now;
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == fa) continue;
get(vj, now, anss ^ sg[vj]);
}
}
int main() {
n = read();
for(int i = 1; i <= n; i++) cor[i] = read();
for(int i = 1; i < n; i++) {
int vi = read(), vj = read();
to[vi].push_back(vj);
to[vj].push_back(vi);
}
dfs(1, 0);
get(1, 0, 0);
if(tot) {
sort(ans + 1, ans + tot + 1);
for(int i = 1; i <= tot; i++) cout << ans[i] << '\n';
} else puts("-1");
return 0;
}
SPOJ COT3 - Combat on a tree的更多相关文章
- 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\)个点的树,每个点是黑色或白色.两个人轮流操作,每次可以选一个白色的点,将它到根节点路径上的所有点染黑.不能操作的人输,求先手是否能赢.如果能 ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- 【BZOJ2589】 Spoj 10707 Count on a tree II
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 【SPOJ】QTREE7(Link-Cut Tree)
[SPOJ]QTREE7(Link-Cut Tree) 题面 洛谷 Vjudge 题解 和QTREE6的本质是一样的:维护同色联通块 那么,QTREE6同理,对于两种颜色分别维护一棵\(LCT\) 每 ...
随机推荐
- 读DataSnap源代码(二)
program Project1; {$APPTYPE GUI} {$R *.dres} uses Vcl.Forms, Web.WebReq, IdHTTPWebBrokerBridge, Form ...
- C++ Builder使用VC DLL
好久没用BCB了,真的有些陌生了,当然个烂笔头吧. 1 先 implib c:\xxx.lib c:\xxx.dll 生成lib文件 2 #pragma comment(lib,"xxx.l ...
- Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用搭建
高可用集群 在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对与微服务和服务注册中心都需要高可用部署,Eureka 高可用实际上就是将自己 ...
- js this详解,事件的三种绑定方式
this,当前触发事件的标签 在绑定事件中的三种用法: a. 直接HTML中的标签里绑定 onclick="fun1()"; b. 先获取Dom对象,然后利用dom对象在js里绑定 ...
- 利用VMware虚拟机安装CentOS7
CentOS7镜像下载:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso VMware虚拟 ...
- nginx-ngx_http_random_index_module
模块简介:在收到以"/"为结尾的请求时,在对应的目录下随机选择一个文件作为index文件. 模块类型:常用标准http模块 使用实例: location /privacy_poli ...
- Java封装案例
---恢复内容开始--- 在类的getter/setter方法中在getter方法中要先实列话 在构造方法中无参构造(必须要构造)好处很多!!! 描述学生信息及相关联的专业信息 把Subject和St ...
- ASP防止SQL注入
'防止SQL注入'http://0.0.0.0/bzhs/login.asp?logType=edit';WAITFOR DELAY '0:0:5' --logType = Replace(Repla ...
- SVN怎么触发Jenkins自动构建
通常,有几种方式可以在SVN仓库发生改变时触发Jenkins进行构建.第一种是,Jenkins主动轮询SVN仓库:第二种是,在SVN客户端(如TortoiseSVN)创建客户端hooks来触发构建:第 ...
- 使用JavaScript完成文字向上间歇滚动
使用JavaScript完成文字的间歇滚动 const init = (initData) => { const area = initData.area; // 设置单行滚动的高度: cons ...