【博弈论+记忆化dp+visit数组+回溯】acm 2014 西安赛区 H problem make you happy
来源:互联网 发布:每年车祸数据统计 编辑:程序博客网 时间:2024/05/01 04:58
代码如下,过了测试数据。
记忆化dp
注意visit[a][b][tag]记录是否之前到达了当前的状态,如果到达过,说明进入了一个环,回溯。
注意回溯的时候,不要设置res[a][b][tag],因为该结果可能尚未产生。
#include<vector>#include<iostream>#include<string.h>using namespace std;// -1: alice win, 1: bob win, -2: initedint res[100][100][2];//[a][b][if a go now]bool visit[100][100][2];// -1: alice win, 1: bob win// a_f: if alice go nowint dfs(vector<vector<int> > &tree,int a, int b, bool a_f){ // cout<<a<<":"<<b<<":"<<a_f<<endl; if(res[a][b][a_f]!=-2) return res[a][b][a_f]; if(a==b) return res[a][b][a_f]=-1; if(visit[a][b][a_f]) return 1; //回溯(res[a][b][a_f]结果未必产生) visit[a][b][a_f] = true; int t=-3; if(a_f){//a go for(int i=0; i<tree[a].size(); i++){ int a1=tree[a][i]; t=dfs(tree, a1, b, false); if(t==-1) break; } // if t==-3 or 1, then bob win res[a][b][a_f]=t==-3?1:t; }else{ for(int i=0; i<tree[b].size(); i++){ int b1=tree[b][i]; t=dfs(tree, a, b1, true); if(t!=-1) break; } // if t==-3 or -1, then alice win res[a][b][a_f]=t==-3?-1:t; } visit[a][b][a_f]=false; return res[a][b][a_f];}int main(){ int cases; cin>>cases; for(int casei=1; casei<=cases; casei++){ int n, m; cin>>n>>m; vector<vector<int> > tree(n, vector<int>()); for(int i=0; i<m; i++){ int a, b; cin>>a>>b; a--, b--; tree[a].push_back(b); } memset(visit, 0, sizeof(visit)); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ for(int k=0; k<2; k++){ res[i][j][k]=-2; } } } int a, b; cin>>b>>a; a--, b--; int s=dfs(tree, a, b, false); cout<<"Case #"<<casei<<": "; if(s==1) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0;}
0 0
- 【博弈论+记忆化dp+visit数组+回溯】acm 2014 西安赛区 H problem make you happy
- CF GYM 100548 The Problem to Make You Happy(2014ACM西安现场赛Problem H)
- UVA7042(2014西安H题)-The Problem to Make You Happy-博弈+bfs
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest(H - The Problem to Make You Happy )
- 【后缀树】acm 2014 西安赛区 Problem G. The Problem to Slow Down You
- Codeforces Gym 100548H The Problem to Make You Happy (图上博弈DP)
- 【树dp】acm 2014 西安赛区 J Unlimited Battery Works
- HDU5014 2014ACM-ICPC 亚洲区域赛西安赛区网络赛H题 Number Sequence
- 2014ACM-ICPC西安赛区网络预赛
- 2014 ACM/ICPC 西安赛区 F -- color
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing(记忆化dfs)
- 2017 ACM/ICPC 乌鲁木齐赛区 H题,dfs 记忆化 O(n)
- ACM 2015西安赛区题目
- 2017ACM-ICPC西安赛区
- 2010 ACM/ICPC 福州赛区 Problem H(贪心)
- 2014 ACM/ICPC 西安赛区网络赛解题报告汇总
- 2014 ACM区域赛 西安赛区现场赛总结
- 2014 西安赛区邀请赛
- 敏捷相关Note
- java实现队列及队列的应用
- myeclipse10.7.1+maven3.23配置
- Java_并发线程_CompletionService
- web.py 0.3 新手指南
- 【博弈论+记忆化dp+visit数组+回溯】acm 2014 西安赛区 H problem make you happy
- RTS/CTS协议
- 第5条:避免创建不必要的对象
- [LeetCode] Two Sum
- mysqldump 备份导出数据排除某张表
- leetcode - Insert Interval
- 坐标下降法(Coordinate descent)
- 贪心算法---心急的C小加/工艺品加工问题
- setsocket函数