Gym - 100548H The Problem to Make You Happy 2014-2015 ACM-ICPC, Asia Xian Regional Contest (BFS+博弈)

题意:Bob和Alice在一张有向无环图上移动,给定二者的起点,Bob先手.Bob的失败条件是不能移动或者与Alice相遇.两个人都采取最优策略,求Bob是否会赢

分析:银牌题.先确定所有的失败状态,然后根据这些反向状态BFS.

用\(dp[i][j][0or1]\)表示bob在i点,Alice在j点,当前移动的人是bob还是Alice的情况, bob是否必败.

首先能确定的是 \(dp[i][j][0] = dp[i][j][1] = 0\), 对于出度为0的点\(i\),\(dp[i][j][0]= 0\).

搜索时有两种状态:

一是当前手是Bob,上一次是Alice,因为两者都选择最优策略,所以Alice肯定会选择让Bob的必败的状态,所以能达到该状态的其余状态都是必败态,将其入队列.

二是当前为Alice,上一次是Bob.Bob肯定不会选择让自己必败的状态,除非它的每一个选择都会走到必败态.统计这个前驱状态会走到的必败态的个数,若此个数等于这个前驱点的出度,表示状态出发的所有状态,Bob都是必败,则这个状态也是必败,将其入队列.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e2+5;
int x,y;
int dp[MAXN][MAXN][2];
int deg[MAXN];                      //出度
int num[MAXN][MAXN];              //失败的标记
struct Edge{
   int v,next;
}E[MAXN*MAXN] ,rE[MAXN*MAXN];
int rhead[MAXN],rtot,N, M;

void init()
{
    memset(num,0,sizeof(num));
    memset(deg,0,sizeof(deg));
    memset(rhead,-1,sizeof(rhead));
    rtot = 0;
}

void rAddEdge(int u,int v)
{
    rE[rtot] = (Edge){v,rhead[u]};
    rhead[u] = rtot++;
}

struct Node{
    int a, b, cur;
};
bool BFS()
{
    memset(dp, -1,sizeof(dp));
    queue<Node> Q;                      //从所有的必败态开始反着搜
    for(int i=1;i<=N;++i){
        Q.push((Node){i,i,0});          //都是必败态
        Q.push((Node){i,i,1});
        dp[i][i][0] = dp[i][i][1] = 0;
        if(!deg[i]){
            for(int j=1;j<=N;++j){
                if(j==i) continue;
                Q.push((Node){i,j,0});
                dp[i][j][0] = 0;
            }
        }
    }

    while(!Q.empty()){
        Node x = Q.front(); Q.pop();
        int a = x.a , b = x.b, cur = x.cur;
        int nxt = cur^1;
        if(!cur){               //Bob
            int u = b;
            for(int i = rhead[u]; ~i ;i = rE[i].next){
                int v = rE[i].v;
                if(dp[a][v][1] ==-1){
                    dp[a][v][1] = 0;
                    Q.push((Node){a,v,1});
                }
            }
        }else{                  //Alice
            int u = a;
            for(int i = rhead[u]; ~i ; i = rE[i].next){
                int v = rE[i].v;
                num[v][b]++;
                if(num[v][b]>= deg[v] && dp[v][b][0]==-1){
                    dp[v][b][0] = 0;
                    Q.push((Node){v,b,0});
                }
            }
        }
    }
    if(dp[x][y][0]==0) return false;
    return true;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    int T,cas=1; scanf("%d",&T);
    while(T--){
        init();
        scanf("%d %d ",&N, &M);
        int u, v;
        while(M--){
            scanf("%d %d",&u, &v);
            rAddEdge(v,u);              //反向建图
            deg[u]++;
        }
        scanf("%d %d", &x , &y);
        printf("Case #%d: ",cas++);
        if(BFS()) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/xiuwenli/p/9744397.html

时间: 2024-11-10 03:08:05

Gym - 100548H The Problem to Make You Happy 2014-2015 ACM-ICPC, Asia Xian Regional Contest (BFS+博弈)的相关文章

(线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 738    Accepted Submission(s): 591 Problem Description In Land waterless, w

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 (Gym - 102082)

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 A - Digits Are Not Just Characters 签到. B - Arithmetic Progressions 题意:从给定的集合中选出最多的数构成等差数列. 题解:数字排序后,设\(dp[i][j]\)表示等差数列最后一个数字为\(a[i]\),倒数第二个数字为\(a[j]\)的最大个数.然后对于每一位枚举 \(i\),\(lower\_bound()\)找有无合法的

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

codeforce gym 100548H The Problem to Make You Happy

题意: Alice和Bob在一个有向图上玩游戏,每个人各自操作一个棋子,如果两个棋子走到一个点上,判定Bob输:如果轮到任何一方走时,无法移动棋子,判定该方输 现在Bob先走,要求判断胜负 题解 模型上看是SG问题,但是通常的SG做法需要DP,但是考虑这不是DAG模型,普通的记忆化搜索写法会RE 正解的DP做法:dp[i][j][k]:i,j是Bob,Alice的位置,k是目前轮到谁走了. 开始将所有显然的Bob输的情况加入队列中,不断拓展,找到所有的Bob输的情况. 转移类似SG #inclu

Hdu 5445 Food Problem (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online)

题目链接: Hdu  5445 Food Problem 题目描述: 有n种甜点,每种都有三个属性(能量,空间,数目),有m辆卡车,每种都有是三个属性(空间,花费,数目).问至少运输p能量的甜点,花费最小是多少? 解题思路: 明显可以看出是多重背包搞两次,但是数据范围太大了,背包要到2*1e6,感觉会TLe.还是呆呆的写了一发,果断超啊!然后滚回去看背包九讲课件了,看到了二进制压缩的时候,感觉可以搞这个题目.试了一下果然AC,原本物品数目是100*100,二进制压缩以后也就是100*log210

CF GYM 100548 The Problem Needs 3D Arrays(2014ACM西安现场赛Problem C)

ProblemC. The Problem Needs 3D Arrays Description A permutation is asequence of integers p1, p2, . . . , pn,consisting of n distinct positive integers and each of them does notexceed n. Assume that r(S) of sequence S denotes the number ofinversions i

CF GYM 100548 The Problem to Make You Happy(2014ACM西安现场赛Problem H)

ProblemH. The Problem to Make You Happy Description Alice and Bob aregood friends, as in every other storyline. One day Alice and Bob areplaying an interesting game. The game is played on a directedgraph with n vertices and m edges, Alice and Bob hav

Gym - 100548C The Problem Needs 3D Arrays

Problem C.   The Problem Needs 3D Arrays Time Limit: 6000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Description A permutation is a sequence of integers p1,p2,...,pn, consisting of n distinct positive integers and each of them does not e

codeforces Gym 100548G - The Problem to Slow Down You 回文树

直接从两棵树的奇根和偶根dfs就可以了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<map> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using names