D - Black and White Tree


Time limit : 2sec / Memory limit : 256MB

Score : 900 points

Problem Statement

There is a tree with N vertices numbered 1 through N. The i-th of the N−1 edges connects vertices ai and bi.

Initially, each vertex is uncolored.

Takahashi and Aoki is playing a game by painting the vertices. In this game, they alternately perform the following operation, starting from Takahashi:

  • Select a vertex that is not painted yet.
  • If it is Takahashi who is performing this operation, paint the vertex white; paint it black if it is Aoki.

Then, after all the vertices are colored, the following procedure takes place:

  • Repaint every white vertex that is adjacent to a black vertex, in black.

Note that all such white vertices are repainted simultaneously, not one at a time.

If there are still one or more white vertices remaining, Takahashi wins; if all the vertices are now black, Aoki wins. Determine the winner of the game, assuming that both persons play optimally.

Constraints

  • 2≤N≤105
  • 1≤ai,biN
  • aibi
  • The input graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a1 b1
:
aN−1 bN−1

Output

Print First if Takahashi wins; print Second if Aoki wins.


Sample Input 1

3
1 2
2 3

Sample Output 1

First

Below is a possible progress of the game:

  • First, Takahashi paint vertex 2 white.
  • Then, Aoki paint vertex 1 black.
  • Lastly, Takahashi paint vertex 3 white.

In this case, the colors of vertices 12 and 3 after the final procedure are black, black and white, resulting in Takahashi's victory.


Sample Input 2

4
1 2
2 3
2 4

Sample Output 2

First

Sample Input 3

6
1 2
2 3
3 4
2 5
5 6

Sample Output 3

Second

貌似几百年没有做题了。。。。

题解见注释

/*
f[x]表示以x为根的子树中,先把x染成白之后对方下一步是否会在x子树中操作
g[x]表示以x为根的子树中,先手是否能获得胜利。 当我们枚举致胜节点为root时,先手能赢当且仅当g[root]=1. 初始化(对于单点):
g[x]=1;
f[x]=0; 转移:
1.f[x]等于所有儿子的g的位或 这个不难理解,因为只要有一个儿子的g为1的话,我们先把x染白,
对方一定会在g为1的这个子树中操作,不然就输了。 2.g[x]等于所有儿子的f的位与 这个也不难理解,因为只有所有儿子的f都为1了,我们才可以依次把每个儿子染白
最后依然有先手优势来染x,然后就赢了hhhh 考虑上述算法仅适用于根固定的情况 ,我们可以把它扩展一下,
第一遍dfs预处理以某个节点为根的函数值,
第二遍dfs在每个节点O(1)计算出函数值。
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#define ll long long
#define maxn 100005
#define pb push_back
using namespace std;
vector<int> son[maxn];
int n,m,g[maxn];
int f[maxn];
bool win=0; void dfs1(int x,int fa){
int sz=son[x].size()-1,to;
f[x]=0,g[x]=1; for(int i=0;i<=sz;i++){
to=son[x][i];
if(to==fa) continue;;
dfs1(to,x);
f[x]|=g[to],g[x]&=f[to];
}
} void dfs2(int x,int fa,int fa_f,int fa_g){
int sz=son[x].size()-1,to;
int hzf[sz+2],hzg[sz+2];
hzf[sz+1]=1,hzg[sz+1]=0; if(g[x]&fa_f) win=1; for(int i=sz;i>=0;i--){
hzf[i]=hzf[i+1];
hzg[i]=hzg[i+1];
to=son[x][i];
if(to==fa) continue; hzf[i]&=f[to];
hzg[i]|=g[to];
} for(int i=0;i<=sz;i++){
to=son[x][i];
if(to==fa) continue; dfs2(to,x,fa_g|hzg[i+1],fa_f&hzf[i+1]);
fa_g|=g[to];
fa_f&=f[to];
}
} int main(){
int uu,vv;
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d%d",&uu,&vv);
son[uu].pb(vv);
son[vv].pb(uu);
} dfs1(1,0);
dfs2(1,0,1,0); if(win) puts("First");
else puts("Second"); return 0;
}

  

AtCoder 2376 Black and White Tree的更多相关文章

  1. Atcoder D - Black and White Tree(树dp+博弈)

    题目链接:http://agc014.contest.atcoder.jp/tasks/agc014_d 题意:有一棵树先手涂白色,后手涂黑色,直到不能再涂为止.涂完后再把所有黑色直接相邻的白色都变成 ...

  2. Codeforces 260D - Black and White Tree

    260D - Black and White Tree 思路:把两种颜色先按值sort一下,最小值肯定是叶子,然后把这个叶子和另外一中颜色的一个最小值的节点连接起来,再把这个节点变成叶子,把值减掉就可 ...

  3. HDU 5905 Black White Tree(树型DP)

    题目链接  Black White Tree 树型DP,设$f[i][j]$为以$i$为根的子树中大小为$j$的连通块中可以包含的最小黑点数目. $g[i][j]$为以$i$为根的子树中大小为$j$的 ...

  4. 2017国家集训队作业[agc014d]Black and White Tree

    2017国家集训队作业[agc014d]Black and White Tree 题意: ​ 有一颗n个点的树,刚开始每个点都没有颜色.Alice和Bob会轮流对这棵树的一个点涂色,Alice涂白,B ...

  5. AtCoder Grand Contest 014 D:Black and White Tree

    题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_d 题目翻译 给你一棵树,每次任选一个点染色,先手染白色,后手染黑色.如果最后存在一个白色的点 ...

  6. 【AtCoder AGC023F】01 on Tree(贪心)

    Description 给定一颗 \(n\) 个结点的树,每个点有一个点权 \(v\).点权只可能为 \(0\) 或 \(1\). 现有一个空数列,每次可以向数列尾部添加一个点 \(i\) 的点权 \ ...

  7. AtCoder Grand Contest 010 F - Tree Game

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...

  8. AtCoder Grand Contest 018 D - Tree and Hamilton Path

    题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d 题目大意: 给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点 ...

  9. AtCoder Grand Contest 005 C - Tree Restoring

    题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c 题目大意: 给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节 ...

随机推荐

  1. 二 APPIUM Android自动化 测试初体验

    本文转自:http://www.cnblogs.com/sundalian/p/5629358.html 1.创建一个maven项目 成功新建工程: 编辑pom.xml,在<dependenci ...

  2. HDU 4699 Editor(双向链表)

    双向链表直接模拟. 用一个辅助数组maxSum来维护一下前k项中[1,k]的最大和. 因为光标是一格一格的移动,所以每次光标右移的时候动态更新一下即可. 时间复杂度O(n). #include < ...

  3. Virtual box 虚拟机 不可使用的一种解决方法

    在win7下开着一个ubuntu虚拟机,写了好几小时代码,然后饿了,去吃点东西 回来发现,电脑由于win7 自动更新已经重启 ubuntu下的代码我已经走开时保存过了,所以不怎么担心 但是打开virt ...

  4. fisheye在centos上的安装

    目录 描述 部署过程 安装及配置 破解 添加存贮库 在jira上配置 描述 Fisheye 一个源代码库深度查看软件,它可以挖掘源代码库中的有用信息,呈现在Web浏览器界面上. Crucible是一个 ...

  5. kvm竟然抓不到kvm的tracepoint

    今天终于把kvm给搭起来了,打开了host机的tracepoint竟然一个都没有抓到,这是咋回事? 难道kvm的东西只有在启动的时候才会被抓到? 虚拟出来一块内存一块CPU,虚拟出来一个内存.感觉都好 ...

  6. web服务器集群session同步

    在做了web集群后,你肯定会首先考虑session同步问题,因为通过负载均衡后,同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,一个登录用户,一会是登录状态,一会又不是 ...

  7. Codeforces 359D Pair of Numbers | 二分+ST表+gcd

    题面: 给一个序列,求最长的合法区间,合法被定义为这个序列的gcd=区间最小值 输出最长合法区间个数,r-l长度 接下来输出每个合法区间的左端点 题解: 由于区间gcd满足单调性,所以我们可以二分区间 ...

  8. 《R语言实战》读书笔记--第五章 高级数据管理

    本章内容: 数学和统计函数 字符处理函数 循环和条件执行 自编函数 数据整合与重塑 5.1一个数据处理难题 5.2数值和字符处理函数 分为数值函数和字符串函数,下面是数学函数截图: ceiling.f ...

  9. php的post

    代码的顺序不能乱,否则会提交错误 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HE ...

  10. COM RTS/CTS, DTR/DSR

    COM: 串行通讯端口cluster communication port它是串行接口,现在的PC 机一般有两个串行口COM 1 和COM 2 .串行口不同于并行口之处在于它的数据和控制信息是一位接一 ...