Board game HDU - 1281 (binary matching, Hungarian algorithm)

Title Link
Sample Input

3 3 4
1 2
1 3
2 1
2 2
3 3 4
1 2
1 3
2 1
3 2

Sample Output

Board 1 have 0 important blanks for 2 chessmen.
Board 2 have 3 important blanks for 3 chessmen.

Question meaning: how many positions can I put the car at most? Suppose I put the car at (x,y) point. Obviously, the other columns in row X and the other rows in column y can't place the car.
Why is this problem a binary matching problem? For example, if you put it in row a0, the column a0 can put the car has b0,b1 bn, for example, in column b0, then a0 matches to b0. If it is placed in column b1, then it can be said that a0 matches to b1. Therefore, (a,b) represents that a matches to B and how many cars to place at most. This problem is to select as many places as possible, that is, to find as many matching sides of a and B as possible, that is, to find the maximum matching of x and y sets
After finding out the maximum number of cars, calculate the important points, enumerate and delete each point that can put cars, and calculate the maximum matching every time. If the maximum matching number at this time is not equal to the previous one, then this point is the important point, because without it, the maximum matching number will change.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int map[110][110],match[110],vis[110];
int x[10100],y[10100];
int n,m;
int Find(int i)
{
    for(int j=1;j<=m;j++)
    {
        if(map[i][j]&&!vis[j])
        {
            vis[j] = 1;
            if(match[j]==0||Find(match[j]))
            {
                match[j] = i;
                return 1;
            }
        }
    }
    return 0;
}
int maxP()//Find the maximum match of x,y sets
{
    int sum = 0;
    memset(match,0,sizeof(match));
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(Find(i))
            sum++;
    }
    return sum;
}
int main()
{
    int k,i;
    int t = 1;
    while(~scanf("%d %d %d",&n,&m,&k))
    {
        memset(map,0,sizeof(map));
        for( i=1;i<=k;i++)
        {
            scanf("%d %d",&x[i],&y[i]);
            map[x[i]][y[i]] = 1;//Can be set to 1
        }
        int ans = maxP();//Find the maximum match of all points
        int sum = 0;
        for(i=1;i<=k;i++)//Enumerate every point
        {
            map[x[i]][y[i]] = 0;//Delete point
            int re = maxP();//Find the maximum match after deleting points
            map[x[i]][y[i]] = 1;//recovery
            if(re < ans)
                sum++;
        }
        printf("Board %d have %d important blanks for %d chessmen.\n",t++, sum, ans);
    }
    return 0;
}

Posted by p2grace on Thu, 02 Apr 2020 07:13:53 -0700