CF GYM 100548 The problem to make you Happy (2014ACM Xian live race problem H)

Source: Internet
Author: User

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 adirectedgraph with n vertices and m edges, Alice and Bob has exactly onechess piece each. Initially, Bob ' s chess piece is placed on Vertexx, while Alice's chess piece is placed at Vertex y. Bob Playsfirst, then Alice, then Bob, then Alice and so on.

During each move,the player must move his/her chess piece from the vertex his/herchess piece currently at-an adjacent V Ertex, by traveling throughexactly one directed edge. (Remember that the game was played on adirected graph.) If someone can ' t make such a move, He/shewill is considered to lose the game.

There ' s oneadditional rule:at any time, if Bob and Alice's Chess pieces areat the same vertex, then Alice was consider to Be the winner and TheGame ends up immediately.

Giventhe initial situation, could you determine? Pleasenote that neither Bob nor Alice would make any mistakes during thegame, i.e. they both play optimally. In case, the game never Endsup, Bob was considered to be the winner.


Input

The first line ofthe input gives the number of test cases, T. t test cases follow.

For each test case,the first line contains the integers n and M (2≤n≤100, 1≤m≤nx (n? 1)). Next m lines, each line contains the INTEGERSB and E, indicating there is one directed edge from Vertex b Tovertex E. Last line contains, integers x and y (1≤x, y≤n, x = y), which is Bob and Alice's initial position. The Graphcontains no self-loops or duplicate edges.


Output

For each test caseoutput a line "case #x: Y", where x was the case number (starting from 1) and y is "Yes" (without quotes If Bob can winor the game never ends up, otherwise "No" (without quotes).


Samples

Sample Input

Sample Output

3

5 3

1 2

3 4

4 5

3 1

4 3

1 2

2 3

3 4

1 2

3 3

1 2

2 3

3 1

2 1

Case #1: Yes

Case #2: No

Case #3: Yes


Knowledge Points:

Graph, game theory, dynamic programming.

Main topic:

Alice and Bob are playing games with each other. Each of them has a pawn, starting at a different point in the graph. Bob is the initiator, the two people take turns to move the pieces, each time can only move the pieces to the adjacent point (along the graph of the forward edge), repeating the process, when the person can not move, he lost (rule 1).

There are two additional rules: at any time, when two pieces reach the same point, Alice wins (rule 2). If the game never ends,Bob wins (rule 3).

Both follow the optimal strategy, take turns moving the pieces, ask who will win in the end. There are no self-loops and heavy edges in the diagram.


Problem Solving Ideas:

According to the nature of game theory, a situation is the necessary and sufficient condition for a winning state, which will become an inevitable state after a certain decision is made, and the necessary and sufficient condition for a situation to be defeated is that the situation will be a winning state regardless of the decision.

You can use dynamic programming to solve problems.

dp[i][j][who]< Span style= "font-size:10pt" > who ( alice/bob) move the pawn while Alice i point , bob Pawn in j point, this state is not the winning state.

when i J who alice 2

because there may be a ring in the diagram, so that the game can not be terminated, so it is necessary to record whether the state has been accessed, if the current state has been accessed, but after recursion its sub-state has not reached the conclusion of this state, But again to visit this state, it means that there is a ring, you should determine the bob win, the game ends. In this case, if who< Span lang= "ZH-CN" to bob 3

if the current state has no sub-state, then the current state is a must-fail state. (Rule 1)

In other cases, it is driven by the nature of game theory. If all the sub-states of the current state are a winning state, then the current state is a must-fail state, and if there is a must-do state in all the sub-states of the current state, then the current state is the winning state.

Finally, if the starting state Dp[x][y][bob] is the winning state , then Bob wins, or Alice wins.


Reference Code:(not AC )

#include <iostream> #include <cstring> #include <cstdio> #include <vector>using namespace std;  const int MAXN = 110;int ncase, Ccase, N, m, x, Y, dp[maxn][maxn][2]; Dp[x][y][who]: -1-initial, 0-lose State, 1-win statevector<int> g[maxn];bool visited[maxn][maxn][2];void I    NIT () {for (int i = 0; i < MAXN; i++) {g[i].clear ();    } memset (DP,-1, sizeof (DP)); Memset (visited, false, sizeof (visited));    void input () {scanf ("%d%d", &n, &m);        for (int i = 0; i < m; i++) {int B, E;        scanf ("%d%d", &b, &e);    G[b].push_back (e); } scanf ("%d%d", &x, &y);}    int DP (int x, int y, int who) {//Who:0-bob, 1-alice if (dp[x][y][who]! =-1) return dp[x][y][who]; if (x = = y) return to who = = 1?    1:0; if (visited[x][y][who]) return who = = 0?    1:0;    Visited[x][y][who] = true;        if (who = = 0) {//BOB int flag = 0; for (int i = 0; i < g[x].size (); i++) {if (DP (G[x][i], y, 1) = = 0)                {flag = 1;            Break    }} dp[x][y][who] = flag;        } else if (who = = 1) {//Alice int flag = 0;                for (int i = 0; i < g[y].size (); i++) {if (DP (x, G[y][i], 0) = = 0) {flag = 1;            Break    }} dp[x][y][who] = flag;    } visited[x][y][who] = false; return dp[x][y][who];}    void Solve () {if (DP (x, y, 0) = = 1) {printf ("Case #%d:yes\n", ++ccase);    } else {printf ("Case #%d:no\n", ++ccase);    }}int Main () {scanf ("%d", &ncase);        while (ncase--) {init ();        Input ();    Solve (); } return 0;}
Welcome to discuss correct.

CF GYM 100548 The problem to make you Happy (2014ACM Xian live race problem H)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.