原文链接https://www.cnblogs.com/zhouzhendong/p/AGC017D.html

题目传送门 - AGC017D

题意

  给定一棵 n 个节点的以节点 1 为根的树。

  两个人在博弈,每次可以删除任意一个子树,不能删掉整个树。最终不能操作的人输。

  问先手是否必胜。

  $n\leq 10^5$

题解

  考虑处理出每一个节点的 SG 值。

  对于节点 x ,显然他的所有子树都是独立的,我们只需要求出所有子树的 SG 值然后异或起来就好了。

  假设 y 为 x 的一个儿子,则节点 y 对于 x 的贡献是什么呢?

  显然不是 SG[y] ,因为 x 到 y 还有一条边。在 y 子树中操作的任何时候都可以直接删除这条边到达状态 0 ,相当于 y 子树的所有状态都连了一条到 0 的边。

  所以 SG'[y] = SG[y] + 1 。

  所以 SG[x] 就是所有的 SG'[y] 的异或值(其中 y 为 x 的儿子)。

代码

#include <bits/stdc++.h>
#define y1 __zzd001
using namespace std;
typedef long long LL;
LL read(){
LL x=0;
char ch=getchar();
while (!isdigit(ch))
ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x;
}
const int N=100005;
int n,sg[N];
vector <int> e[N];
void solve(int x,int pre){
sg[x]=0;
for (auto y : e[x])
if (y!=pre){
solve(y,x);
sg[x]^=sg[y]+1;
}
}
int main(){
n=read();
for (int i=1;i<n;i++){
int x=read(),y=read();
e[x].push_back(y);
e[y].push_back(x);
}
solve(1,0);
puts(sg[1]?"Alice":"Bob");
return 0;
}

  

AtCoder Grand Contest 017D (AGC017D) Game on Tree 博弈的更多相关文章

  1. AtCoder Grand Contest 023 F - 01 on Tree

    Description 题面 Solution HNOI-day2-t2 复制上去,删点东西,即可 \(AC\) #include<bits/stdc++.h> using namespa ...

  2. Atcoder Grand Contest 026 (AGC026) F - Manju Game 博弈,动态规划

    原文链接www.cnblogs.com/zhouzhendong/AGC026F.html 前言 太久没有发博客了,前来水一发. 题解 不妨设先手是 A,后手是 B.定义 \(i\) 为奇数时,\(a ...

  3. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  4. AtCoder Grand Contest 005

    AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...

  5. AtCoder Grand Contest 014

    AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中 ...

  6. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  7. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  8. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  9. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

随机推荐

  1. Java RMI与RPC的区别

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6542811.html  一:RPC 远程过程调用 RPC(Remote Procedure Call Prot ...

  2. JS 操作数组对象

    我们在操作数组时,加入数组中是以对象的形式存在,例如: 那么我们会涉及到去重复,去掉为0的数组中的对象,js代码如下: function getItemList(gid, totalMoney, ad ...

  3. js——类型转换

      总述                   类型转换:显式.隐式(自动) 隐式转换是由编译器自动进行的,基本主要抽象操作有ToPrimitive, ToNumber, ToString, ToBoo ...

  4. Oracle Vm VirtualBox 搭建 yum 环境

    配置本地yum库(用root用户操作) 创建挂载目录 #创建挂载目录 [root@oracle ~]# mkdir /mnt/cdrom # 查看挂载目录 [root@oracle ~]# ls /m ...

  5. Confluence 6 配置手动备份

    如果你希望关闭自动备份,你可以选择手动导出保存站点.请参考 Manually Backing Up the Site 页面中的内容获得更多的信息. 这些文件没有自动备份在同样的路径中,这些文件存储在 ...

  6. Confluence 6 用户目录图例 - 使用 LDAP 授权的内部目录

    上面的图:Confluence 连接 LDAP 服务器仅用做授权 https://www.cwiki.us/display/CONFLUENCEWIKI/Diagrams+of+Possible+Co ...

  7. mysql数据库之基本操作和存储引擎

    一.知识储备 数据库服务器:一台计算机(对内存要求比较高) 数据库管理系统:如mysql,是一个软件 数据库:oldboy_stu,相当于文件夹 表:student,scholl,class_list ...

  8. LeetCode(100):相同的树

    Easy! 题目描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 ...

  9. github文档

    Video Guides GitHub Help GitHub.com   Hello World 10 minute read Intro What is GitHub? Create a Repo ...

  10. 多线程相关-ThreadPoolExecutor

    应用层面: ThreadPoolExecutor: 创建多线程池执行器:new ThreadPoolExecutor(),创建方法最终都是走的以下这个构造方法: /** * Creates a new ...