gym 102028 H The Problem to Make You Happy (有向图博弈)

题意

给定一张有向图,以及A B的起始点,满足规则若在一个时刻A与B在同一个位置,则B输,轮到了当无路可走则输,若无限循环则B赢。B先手

题解

维护B的必败点,定义状态,vis[u][v][0/1]代表B在u,A在v,0代表该轮轮到B,1代表该轮轮到A。可知初始状态vis[i][i][0&1]vis[u][1~n][0](if outdeg[u]==0) 这些点为必败点。
考虑从已知点扩展必败点,若该轮为B轮次,且B必败,那么上轮必为A轮次,只要A能通过一点走到该点则上一点的A轮次对于B来说必败,可以加入必败队列,对于轮次为A轮次,且B必败,那么上一轮次一定为B轮次,假若对于B点上一个位置,走任何一个位置都是必败点那么上一个位置对于对于B必败,可加入队列中(这里可以用标记数组来实现)因此只要从初始化点BFS扩点即可

代码

/**
 *     author:     TelmaZzzz
 *     create:     2019-09-21-19.51.22
**/
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(db &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const db &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define erp(x,y,z) for(int x=y;x>=z;x--)
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
#define Fi first
#define Se second
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=3e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//逆元
int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
void TelmaZzzz(){
#ifndef ONLINE_JUDGE
    freopen("1.txt","r",stdin);
#endif
}
int out[N];
int cnt[110][110];
bool vis[110][110][3];
queue<pair<pair<int,int>,int> >q;
int main(){
    TelmaZzzz();
    //ios::sync_with_stdio(false);
    int t;
    R(t);
    int S,T;
    int ti=0;
    int u,v;
    int n,m;
    while(t--){
        R(n,m);
        tot=0;
        while(q.size())q.pop();
        rep(i,1,n){
            out[i]=0;
            head[i]=0;
            rep(j,1,n){
                vis[i][j][0]=vis[i][j][1]=false;
                cnt[i][j]=0;
            }
        }
        rep(i,1,m){
            R(u,v);
            link(v,u);
            out[u]++;
        }
        rep(i,1,n){
            if(!out[i]){
                rep(j,1,n){
                    if(!vis[i][j][0]){
                        q.push(MP(MP(i,j),0));
                        vis[i][j][0]=true;
                        //cout<<i<<' '<<j<<endl;
                    }
                }
            }
            if(!vis[i][i][0]){
                q.push(MP(MP(i,i),0));
                vis[i][i][0]=true;
            }
            if(!vis[i][i][1]){
                q.push(MP(MP(i,i),1));
                vis[i][i][1]=true;
            }
            //vis[i][i][0]=vis[i][i][1]=true;
        }
        R(S,T);
        while(q.size()){
            int u=q.front().Fi.Fi;
            int v=q.front().Fi.Se;
            int turn=q.front().Se;
            q.pop();
            if(turn){
                for(int i=head[u];i;i=NEXT[i]){
                    int y=ver[i];
                    cnt[y][v]++;
                    if(cnt[y][v]==out[y]){
                        if(vis[y][v][0]) continue;
                        vis[y][v][0]=true;
                        //cout<<u<<' '<<v<<' '<<turn<<' '<<y<<' '<<v<<endl;
                        q.push(MP(MP(y,v),0));
                    }
                }
            }
            else {
                for(int i=head[v];i;i=NEXT[i]){
                    int y=ver[i];
                    if(vis[u][y][1]) continue;
                    vis[u][y][1]=true;
                    //cout<<u<<' '<<v<<' '<<turn<<' '<<u<<' '<<y<<endl;
                    q.push(MP(MP(u,y),1));

                }
            }
        }
        printf("Case #%d: ",++ti);
        if(vis[S][T][0]) puts("No");
        else puts("Yes");
    }

    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章