ACD-1403-graph game (Game + bipartite graph maximum matching)

Source: Internet
Author: User

Q: N and P are playing games. N has N1 points, P has N2 points, and N has m undirected edges between P points. Place a stone on one of the points, n first move the stone, move it along the side once, the point before the stone moves and the side connected to the point are deleted, and then move the stone to P, whoever can't move will lose. Output the outcome for each initial position (1 ≤ N1; N2 ≤ 500, 0 ≤ m ≤ 50 000 ).

Question link: http://acdream.info/problem? PID = 1, 1403

--> There are many types of maximum matching for a bipartite graph. However, there may be some vertices. No matter which of the largest matching schemes, they are all built points ..

Then, as long as the first hand moves along the matching edge from such a point, the latter hand can be forced to go nowhere .. (Why? First, move from A to B along the matching edge, and then from B to another c. Suppose C is not built, then, a matching edge A-B with the largest match can be changed to B-C, so a is not necessarily a built Point and does not meet our prerequisites .. Therefore, C must have been built, so first you can continue along the matching side, and finally destroy the opponent)

As a result, the DFS on both sides can find such a point twice ..

#include <cstdio>#include <cstring>const int MAXN = 1000 + 10;const int MAXM = 50000 + 10;struct EDGE{    int to;    int nxt;} edge[MAXM << 1];int n1, n2, m;int hed[MAXN], ecnt;int S[MAXN], T[MAXN];bool vis[MAXN];bool maxMatch[MAXN];void Init(){    ecnt = 0;    memset(hed, -1, sizeof(hed));}void AddEdge(int u, int v){    edge[ecnt].to = v;    edge[ecnt].nxt = hed[u];    hed[u] = ecnt++;}void Read(){    int u, v;    while (m--)    {        scanf("%d%d", &u, &v);        AddEdge(u, v + n1);        AddEdge(v + n1, u);    }    memset(maxMatch, 0, sizeof(maxMatch));}bool Match(int u){    for (int e = hed[u]; e != -1; e = edge[e].nxt)    {        int v = edge[e].to;        if (!vis[v])        {            vis[v] = true;            int temps = S[u];            int tempt = T[v];            S[u] = v;            T[v] = u;            if (tempt == -1 || Match(tempt)) return true;            T[v] = tempt;            S[u] = temps;        }    }    return false;}bool Judge(int u){    vis[u] = true;    if (S[u] == -1) return true;    u = S[u];    for (int e = hed[u]; e != -1; e = edge[e].nxt)    {        int v = edge[e].to;        if (!vis[v] && Judge(v)) return true;    }    return false;}void GetMaxMatchPointLeft(){    memset(S, -1, sizeof(S));    memset(T, -1, sizeof(T));    for (int i = 1; i <= n1; ++i)    {        memset(vis, 0, sizeof(vis));        if (Match(i))        {            maxMatch[i] = true;        }    }    for (int i = 1 + n1; i <= n2 + n1; ++i)    {        if (T[i] != -1)        {            memset(vis, 0, sizeof(vis));            if (Judge(T[i]))            {                maxMatch[T[i]] = false;            }        }    }}void GetMaxMatchPointRight(){    memset(S, -1, sizeof(S));    memset(T, -1, sizeof(T));    for (int i = 1 + n1; i <= n2 + n1; ++i)    {        memset(vis, 0, sizeof(vis));        if (Match(i))        {            maxMatch[i] = true;        }    }    for (int i = 1; i <= n1; ++i)    {        if (T[i] != -1)        {            memset(vis, 0, sizeof(vis));            if (Judge(T[i]))            {                maxMatch[T[i]] = false;            }        }    }}void Output(){    for (int i = 1; i <= n1; ++i)    {        maxMatch[i] ? putchar('N') : putchar('P');    }    puts("");    for (int i = 1 + n1; i <= n2 + n1; ++i)    {        maxMatch[i] ? putchar('N') : putchar('P');    }    puts("");}int main(){    while (scanf("%d%d%d", &n1, &n2, &m) == 3)    {        Init();        Read();        GetMaxMatchPointLeft();        GetMaxMatchPointRight();        Output();    }    return 0;}


ACD-1403-graph game (Game + bipartite graph maximum matching)

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.