hdu 3197
来源:互联网 发布:移动4g数据卡 编辑:程序博客网 时间:2024/04/28 05:52
hdu 3197
题意:
给一棵树,两个人每次砍树干,最后没得砍的那个人输,试着判断谁输谁赢。
TIP:
输入很坑,-1代表该点就在地上。
思路:
树形博弈,递归求解sg值即可。NIM博弈。
代码:
#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<queue>#include<map>#include<stack>#include<algorithm>using namespace std;int n;vector<int >ve[1007],s;int res=0;int dfs(int now,int pre){ int res=0; for(int i=0;i<ve[now].size();i++) { int t=ve[now][i]; if(t!=pre) res=res^(1+dfs(t,now)); } return res;}int main(){ while(scanf("%d",&n)!=EOF) { for(int i=0;i<=n;i++) ve[i].clear(); s.clear(); for(int i=0;i<n;i++) { int x; scanf("%d",&x); if(x==-1) { s.push_back(i); } else { ve[x].push_back(i); ve[i].push_back(x); } } res=0; for(int i=0;i<s.size();i++) { res=res^dfs(s[i],-1); } if(res==0) { puts("NO"); } else puts("YES"); } return 0;}
0 0
- hdu 3197
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- 常量指针和指针常量
- UESTC 1170 红蓝点对
- POJ-3414-Pots
- JavaScript强化教程——Cocos2d-JS中JavaScript继承
- scrapy笔记(3)-微博模拟登录及抓取微博内容
- hdu 3197
- 144. Binary Tree Preorder Traversal
- mysqli连接数据库
- 2016/08/11 多态
- HDU1542 线段树+离散化+扫描线
- ArrayList的使用方法
- C++ Socket 一个服务器 多个客户端 (阻塞式)
- 3000门徒内部训练绝密视频(泄密版)第6课:精通Spark集群搭建与测试
- 钱仓【NOIP2016提高A组8.11】