HDU1281 board game (build a bipartite map)

board game



Problem Description
Xiaoxi and Gardon are playing a game: for an N*M chessboard, put as many "rooks" in chess as possible in the grid, and make them unable to attack each other. This is of course very simple, but Gardon limits only Only certain grids can be placed. Xiaoxi solved this problem very easily (see the picture below). Note that the places where the cars cannot be placed will not affect the mutual attack of the cars.
So now Gardon wants Xiaoxi to solve a more difficult problem. On the premise of ensuring as many "rooks" as possible, some squares in the chessboard can be avoided. That is to say, if you don't put rooks on these squares, you can Make sure that as many "cars" as possible are put down. However, if some grids are not placed, it is impossible to guarantee as many "cars" as possible. Such grids are called important points. Gardon wants Xiaoxi to figure out how many such important points there are. Can you solve this problem?
 

Input
The input contains multiple sets of data,
the first line has three numbers N, M, K (1<N,M<=100 1<K<=N*M), which indicate the height and width of the chessboard, and the "car" that can be placed number of grids. The next K lines describe all the grid information: each line has two numbers X and Y, which indicate the position of the grid on the board.
 

Output
For each set of input data, output in the following format:
Board T have C important blanks for L chessmen.
 

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.

Idea: Since there can only be one more piece in each row and column, a bipartite graph can be built, and the rows and columns can be built with edges. The graphs thus built can be enumerated and deleted separately to see if the maximum number of matches is reduced. The reduction is the important point. .

Code:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

int n,m,k,t;
int mp[520][520];
int edge[520][2];
int g[520],vis[520];

int find(int x)
{
	for(int i = 1;i<= m;i++)
	{
		if(mp[x][i]&&!vis[i])
		{
			show [i] = 1;
			if(!g[i]||find(g[i]))
			{
				g[i] = x;	
				return 1;
			}	
		}
	}
	
	return 0;
}

void init()
{
	mem(mp,0);
	mem(g,0);
	t = 0;
}

intmain()
{
	int cnt = 0;
	while(~scanf("%d %d %d",&n,&m,&k))
	{
		init();
		
		for(int i = 1;i<= k;i++)
		{
			int a,b;
			scanf("%d %d",&a,&b);
			mp [a] [b] = 1;
		}
		
		int sum = 0,ans = 0;
		for(int i = 1;i<= n;i++)
		{
			mem(vis,0);
			if(find(i))
				sum++;
		}
		
		for(int i = 1;i<= m;i++)
		{
			if(!g[i])
				continue;
			edge[++t][0] = g[i];
			edge[t][1] = i;
		}
		
		for(int i = 1;i<= t;i++)
		{
			mp [edge [i] [0]] [edge [i] [1]] = 0;
			
			int num = 0;
			mem(g,0);
			for(int j = 1;j<= n;j++)
			{
				mem(vis,0);
				if(find(j))
					num++;
			}
			
			if(num< sum)
				years++;
			mp [edge [i] [0]] [edge [i] [1]] = 1;
		}
		
		printf("Board %d have %d important blanks for %d chessmen.\n",++cnt,ans,sum);
	}
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325903011&siteId=291194637