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;
}

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