UVALive - 7042 The Problem to Make You Happy 博弈

來源:互聯網
上載者:User

標籤:狀態   back   mes   while   empty   情況   for   std   否則   

題目大意:給你一個有向圖, Bob 和 Alice 在做遊戲,每輪他們走一步,當Bob 和 Alice在同一個點或者 Bob無路可走,Bob輸,否則Alice輸。

 

思路:因為在Bob贏的時候存在有環的情況, 但是在Bob輸的時候的狀態是明確的,我們利用Bob輸的狀態進行必勝比敗態推演,

f[ i ][ j ][ k ] 表示Alice在i ,Bob在j 且輪到k走  Bob是必輸還是必勝。

#include<bits/stdc++.h>#define LL long long#define fi first#define se second#define mk make_pair#define pii pair<int, int>#define y1 skldjfskldjg#define y2 skldfjsklejgusing namespace std;const int N = 100 + 7;const int M = 1e5 + 7;const int inf = 0x3f3f3f3f;const LL INF = 0x3f3f3f3f3f3f3f3f;const int mod = 1e9 +7;int n, m, B, A, deg[N], num[N][N][2];vector<int> edge[N], redge[N];int f[N][N][2], vis[N][N][2];struct node {    node(int x, int y, int d) {        this->x = x;        this->y = y;        this->d = d;    }    int x, y, d;};void init() {    for(int i = 0; i < N; i++) {        edge[i].clear(); redge[i].clear();    }    memset(deg, 0, sizeof(deg));    memset(vis, 0, sizeof(vis));    memset(num, 0, sizeof(num));    for(int i = 0; i < N; i++)        for(int j = 0; j < N; j++)            f[i][j][0] = f[i][j][1] = 1;}void bfs() {    queue<node> que;    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            for(int k = 0; k < 2; k++) {                if(i == j) {                    f[i][j][k] = 0;                    que.push(node(i, j, k));                    vis[i][j][k] = 1;                } else if(k == 1) {                    if(!edge[j].size()) {                        f[i][j][k] = 0;                        que.push(node(i, j, k));                        vis[i][j][k] = 1;                    }                }            }        }    }    while(!que.empty()) {        node cur = que.front(); que.pop();        if(!cur.d) {            for(int i = 0; i < redge[cur.y].size(); i++) {                int nxy = redge[cur.y][i];                if(vis[cur.x][nxy][1]) continue;                num[cur.x][nxy][1]++;                if(num[cur.x][nxy][1] == deg[nxy]) {                    f[cur.x][nxy][1] = 0;                    vis[cur.x][nxy][1] = 1;                    que.push(node(cur.x, nxy, 1));                }            }        } else {            for(int i = 0; i < redge[cur.x].size(); i++) {                int nxx = redge[cur.x][i];                if(vis[nxx][cur.y][0]) continue;                f[nxx][cur.y][0] = 0;                vis[nxx][cur.y][0] = 1;                que.push(node(nxx, cur.y, 0));            }        }    }}int main() {    int T; scanf("%d", &T);    for(int cas = 1; cas <= T; cas++) {        init();        scanf("%d%d", &n, &m);        for(int i = 1; i <= m; i++) {            int u, v; scanf("%d%d", &u, &v);            edge[u].push_back(v);            redge[v].push_back(u);            deg[u]++;        }        scanf("%d%d", &B, &A);        bfs();        printf("Case #%d: ", cas);        printf("%s\n", f[A][B][1] ? "Yes" : "No");    }    return 0;}/**/

 

UVALive - 7042 The Problem to Make You Happy 博弈

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.