HDU 1281 board game row and column match

Source: Internet
Author: User

Source: HDU 1281 board games

Some points cannot be attacked when they can be put in the car. Find out which points must be put in the car and cannot get the largest match.

Ideas: each vertex in the row-column matching matrix first finds the largest matching ans for each edge of the Bipartite Graph. If it is time-consuming to remove a vertex and then re-find the largest matching, the first bipartite matching graph is saved.

Then the key points must be the matching edge enumeration to remove the grid point (that is, to remove a matched edge). If it can still be matched, the grid point is not the key point. Instead, do not repeat the maximum match each time.

#include 
 
  #include 
  
   #include 
   
    using namespace std;const int maxn = 110;const int maxm = 10010;bool vis[maxn];int y[maxn];int n, m;int a[maxn][maxn];int b[maxn];bool dfs(int u){    for(int i = 0; i < m; i++)    {        int v = i;        if(vis[v] || !a[u][i])            continue;        vis[v] = true;        if(y[v] == -1 || dfs(y[v]))        {            y[v] = u;            return true;        }    }    return false;}int match(){    int ans = 0;    memset(y, -1, sizeof(y));    for(int i = 0; i < n; i++)    {        memset(vis, false, sizeof(vis));        if(dfs(i))            ans++;    }    return ans;}int main(){    int cas = 1;    int T;    int k;    //scanf("%d", &T);    while(scanf("%d %d %d", &n, &m, &k) != EOF)    {        memset(a, 0, sizeof(a));        memset(b, 0, sizeof(b));                while(k--)        {            int u, v;            scanf("%d %d", &u, &v);            u--;            v--;            a[u][v] = 1;        }            int ans = match();           int sum = 0;               for(int i = 0; i < m; i++)        {                memset(b, 0, sizeof(b));            for(int j = 0; j < m; j++)                   if(y[j] != -1)                       b[y[j]] = 1;            if(y[i] == -1)                continue;            int temp = y[i];            y[i] = -1;            a[temp][i] = 0;            b[temp] = 0;            int flag = 0;            for(int j = 0; j < n; j++)            {                memset(vis, 0, sizeof(vis));                if(!b[j] && dfs(j))                    flag = 1;                if(flag)                    break;            }            if(!flag)            {                sum++;                y[i] = temp;            }            a[temp][i] = 1;        }                   printf("Board %d have %d important blanks for %d chessmen.\n", cas++, sum, ans);    }    return 0;}
   
  
 



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.