Codeforces 812E Sagheer and Apple Tree ——(阶梯博弈)
之前在bc上做过一道类似的阶梯博弈的题目,那题是移动到根,这题是移动到叶子。换汤不换药,只要和终态不同奇偶的那些位置做nim即可。因此这题给出了一个条件:所有叶子深度的奇偶性相同。同时需要注意的是,上次bc中,根节点是不能移动的,因此根节点是终态节点,而这里叶子上面还可以进行操作(可以吃掉),那么就相当于叶子节点都还可以继续向下移动,因此他们不是终态节点,也就是说这题只要和叶子节点同奇偶的做nim即可。
因此,如果nim和已经是0,已经可以满足先手必输了,而题目说了必须要交换,那么只要让奇偶性相同的节点做交换即可,统计一下奇偶节点的个数再C(cnt, 2)就做完了;否则,后手必须要把和叶子节点不同奇偶的换过去来使得nim和为0,利用异或的性质以及map就可以做出来了。具体见代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
const int N = 1e5 + ;
typedef long long ll; int a[N];
vector<int> G[N];
int n;
int ji = -;
void dfs(int u,int fa,int deep)
{
if(ji != -) return ;
int flag = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v != fa)
{
flag = ;
dfs(v, u, deep + );
}
}
if(flag == )
{
ji = deep % ;
}
}
vector<int> yes, no;
void dfs2(int u,int fa,int deep)
{
if(deep % == ji) yes.push_back(a[u]);
else no.push_back(a[u]);
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v != fa)
{
dfs2(v, u, deep + );
}
}
}
ll comb(int x) {if(x < ) return ; return (ll)x*(x-) / ;} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",a+i);
for(int i=;i<=n;i++)
{
int v;
scanf("%d",&v);
G[i].push_back(v);
G[v].push_back(i);
}
dfs(, -, );
dfs2(, -, );
int temp = ;
for(int i=;i<yes.size();i++) temp ^= yes[i];
if(temp == )
{
ll ans = comb(yes.size()) + comb(no.size());
map<int,int> inyes;
for(int i=;i<yes.size();i++) inyes[yes[i]]++;
for(int i=;i<no.size();i++) ans += (ll)inyes[no[i]];
cout << ans << endl;
}
else
{
ll ans = ;
map<int,int> inno;
for(int i=;i<no.size();i++) inno[no[i]]++;
for(int i=;i<yes.size();i++)
{
ll t2 = temp ^ (yes[i]);
ans += inno[t2];
}
cout << ans << endl;
}
return ;
}
Codeforces 812E Sagheer and Apple Tree ——(阶梯博弈)的更多相关文章
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- CodeForces 812E Sagheer and Apple Tree 树上nim
Sagheer and Apple Tree 题解: 先分析一下, 如果只看叶子层的话. 那么就相当于 经典的石子问题 nim 博弈了. 那我们看非叶子层. 看叶子层的父亲层. 我们可以发现, 如果从 ...
- Codeforces 812E Sagheer and Apple Tree
大致题意: 给你一颗树,这个树有下列特征:每个节点上有若干个苹果,且从根节点到任意叶子节点的路径长度奇偶性相同. 甲和乙玩(闲)游(得)戏(慌). 游戏过程中,甲乙轮流将任意一个节点的若干个苹果移向它 ...
- codeforces 812 E. Sagheer and Apple Tree(树+尼姆博弈)
题目链接:http://codeforces.com/contest/812/problem/E 题意:有一颗苹果树,这个苹果树所有叶子节点的深度要不全是奇数,要不全是偶数,并且包括根在内的所有节点上 ...
- Codeforces Round #417 (Div. 2)A B C E 模拟 枚举 二分 阶梯博弈
A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces 348B - Apple Tree
348B - Apple Tree 我们设最后答案为 x , 我们我们就能用x表示出所有节点下面的苹果个数, 然后用叶子节点求lcm, 取最大的可行解. #include<bits/stdc++ ...
- cf202-div 1-B - Apple Tree:搜索,数论,树的遍历
http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory l ...
- POJ 2486 Apple Tree
好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
随机推荐
- [C#] LINQ之SelectMany和GroupJoin
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 三、eureka服务端获取服务列表
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 eureka服务端维护了一个服务信息的列表,服务端节点之间相互复制服务信息.而作为eur ...
- Go 缓冲信道
缓冲信道 语法结构:cap为容量 ch := make(chan type, cap) 缓冲信道支持len()和cap(). 只能向缓冲信道发送容量以内的数据. 只能接收缓冲信道长度以内的数据. 缓冲 ...
- c#系统预定义类型
- VUE目录
学前预备知识 ECMAScript简介和ES6的新增语法 Nodejs基础 webpack的介绍 babel简介 vue基础 vue基础
- 08 Windows编程——画图
源码 #include<Windows.h> #include<tchar.h> #include<stdio.h> #define NUM 1000 LRESUL ...
- pip安装超时:Read timed out.
环境:win10 和 pip 在pip install h5py(或者其他第三方依赖包时) 会出现Read timed out.的问题,即安装超时.如下图所示: 解决方法: 1. 在用户目录下,新建p ...
- B进制星球(多进制 高精加)
https://www.luogu.org/problemnew/show/P1604 B(2<=B<=36)进制计数.编写实现B进制加法的程序. 输入输出格式 输入格式: 共3行第1行: ...
- 图片转换成十六进制TXT文件
最近学习了图片的转换,就学习了一下图片从二进制转换成十六进制,十六进制TXT文件转换成图片形式. using System; using System.Collections.Generic; usi ...
- JQuery中 text()、html() 以及 val()以及innerText、innerHTML和value
设置内容 - text().html() 以及 val() 我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容( ...