您的位置:首页 > 其它

poj 2446 (二分图匹配)

2014-03-23 20:28 309 查看
Description

Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2 to cover the board. However, she thinks it too easy to bob, so she makes some holes on the board (as shown in the
figure below).



We call a grid, which doesn’t contain a hole, a normal grid. Bob has to follow the rules below:

1. Any normal grid should be covered with exactly one card.

2. One card should cover exactly 2 normal adjacent grids.

Some examples are given in the figures below:




A VALID solution.




An invalid solution, because the hole of red color is covered with a card.




An invalid solution, because there exists a grid, which is not covered.

Your task is to help Bob to decide whether or not the chessboard can be covered according to the rules above.
Input

There are 3 integers in the first line: m, n, k (0 < m, n <= 32, 0 <= K < m * n), the number of rows, column and holes. In the next k lines, there is a pair of integers (x, y) in each line, which represents a hole in the y-th row, the x-th column.
Output

If the board can be covered, output "YES". Otherwise, output "NO".
Sample Input
4 3 2
2 1
3 3

Sample Output
YES

Hint




题意是说n*m的棋盘,其中有k个洞,现在给你1*2的纸片,如果能够全部覆盖棋盘且不覆盖洞,而且棋盘的每个格子都被覆盖一次,就输出YES,否则输出NO。

把这个棋盘看成是黑白相间的棋盘,任何相邻的两个格子肯定不是同一种颜色的。如果一个格子的行数和列数加起来为奇数(偶数)那么与它相邻的格子的行数和列数加起来肯定是偶数(奇数)。那么这样的话就可以把整个棋盘分为奇数集合和偶数集合。然后在加边的时候,往上加或者往左加就可以了

#include<stdio.h>
#include<string.h>
#define M 2007
int link[M],head[M],node;
bool vis[M],map[M][M];
struct E
{
    int v,next;
}edg[M*M];
int n,m,k;
void addedge(int u,int v)
{
    edg[node].v=v;
    edg[node].next=head[u];
    head[u]=node++;
}
bool find(int u)
{
    for(int i=head[u];i!=-1;i=edg[i].next)
    {
        int v=edg[i].v;
        if(!vis[v])
        {
            vis[v]=true;
            if(!link[v]||find(link[v]))
            {
                link[v]=u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        memset(map,true,sizeof(map));
        memset(link,0,sizeof(link));
        memset(head,-1,sizeof(head));
        if((n*m-k)&1){printf("NO\n");continue;}
        int a,b,count=0;
        node=0;
        for(int i=0;i<k;i++)
        {
            scanf("%d%d",&b,&a);
            map[a-1][b-1]=false;
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(map[i][j])
                {
                    if(i-1>=0&&map[i-1][j])
                    {
                        if((i+j)&1)addedge(i*m+j,(i-1)*m+j);
                        else addedge((i-1)*m+j,i*m+j);
                    }
                    if(j-1>=0&&map[i][j-1])
                    {
                        if((i+j)&1)addedge(i*m+j,i*m+j-1);
                        else addedge(i*m+j-1,i*m+j);
                    }
                }
            }
        for(int i=0;i<n*m;i++)
        {
            memset(vis,false,sizeof(vis));
            if(find(i))count++;
        }
        //printf("coutn==%d\n",count);
        if(count==(n*m-k)/2)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <cstring>
#define clr(x) memset(x, 0, sizeof(x))

int v1, v2;
bool G[1500][1500];
bool vis[1500];
int link[1500];
int sum;

bool dfs(int x)
{
    for(int y = 1; y <= v2; ++y)
        if(G[x][y] && !vis[y]){
            vis[y] = true;
            if(link[y] == 0 || dfs(link[y])){
                link[y] = x;
                return true;
            }
        }
    return false;
}

void search()
{
    clr(link);
    sum = 0;
    for(int x = 1; x <= v1; ++x){
        clr(vis);
        if(dfs(x))
            sum++;
    }
    return;
}

int main()
{

    int i, j;
    int arr[35][35];
    int m, n, k;
    int x, y;
    int tmpn, tmpm;
    while(scanf("%d %d %d", &n, &m, &k) != EOF)
    {
        memset(arr, -1, sizeof(arr));
        clr(G);
        v1 = v2 = 0;

        for(i = 0; i < k; ++i){
            scanf("%d %d", &x, &y);
            arr[y][x] = 0;///!!!
        }

        if((n*m-k)%2 != 0){
            printf("NO\n");
            continue;
        }

        for(i = 1; i <= n; ++i)
        for(j = 1; j <= m; ++j){
            if(arr[i][j] == -1)
            {
                if((i+j)%2 == 1)
                    arr[i][j] = ++v1;
                else
                    arr[i][j] = ++v2;
            }
        }

        for(i = 1; i <= n; ++i)
        for(j = 1; j <= m; ++j){
            if((i+j)%2 == 0 || arr[i][j] < 1) continue;
            if(arr[i-1][j] >= 1)
                G[arr[i][j]][arr[i-1][j]] = true;
            if(arr[i+1][j] >= 1)
                G[arr[i][j]][arr[i+1][j]] = true;
            if(arr[i][j-1] >= 1)
                G[arr[i][j]][arr[i][j-1]] = true;
            if(arr[i][j+1] >= 1)
                G[arr[i][j]][arr[i][j+1]] = true;
        }
        search();
//printf("%d\n", sum);
        if(sum == (n*m-k)/2) puts("YES");
        else puts("NO");
    }

    return 0;
}





不理解这种写法为什么WA:
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<stack>
#include<queue>
#include <iomanip>
#include<iostream>
#include<algorithm>
using namespace std ;

int M[110][110];
int match[250],f[250];
int vn,un;
struct node
{
	int x,y;
}X[250],Y[250]; 

int juges(int x1,int y1 , int x2 , int y2)
{
	if( (x1%2)==y1%2 && (x2%2==y2%2))
	   return 0;
	else if( abs(x1-x2)<=1 && abs(y1-y2)<=1 )
	         return 1 ;
	 	  else   return 0;	    
}

int dfs(int u)
{
	for( int  v = 1 ; v <= vn ; v++)
	{
		if(juges( X[u].x, X[u].y, Y[v].x, Y[v].y) && !f[v])
		{
			   f[v]=1;
			   if( match[v]==-1 || dfs( match[v]))
			   {
			   	       match[v]=u;
			   	       return 1;
			   }
		}
	}
	return 0;
}

int main()
{
	int  n , m , k ,x,y;
     while(~scanf("%d%d%d",&n,&m,&k))
   {
	memset(M,0,sizeof(M));
	memset(match,-1,sizeof(match));
	while(k--)
	{
		scanf("%d%d",&x,&y);
		M[x][y]=-1;
	}
	 un=0;vn=0;
	 int num = 0;
	for(int i = 1 ; i <= n ; i++)
	    for( int j = 1 ; j <= m ; j++)
	    {   
	      if(M[i][j]!=-1)
	    	{
	    	   if( (i%2)==(j%2) )
	    	     {
	    	  	     un++;
	    	  	     X[un].x=i;X[un].y=j; 
	    	     }
	    	   else
	    	     {
	    	 	   vn++;
	    	 	   Y[vn].x=i;Y[vn].y=j; 
	    	     }
	    	     num++;
	        }
	    }
	    int ans=0;
	for(int i = 1 ; i <= un ; i++)
	{
		memset(f,0,sizeof(f));
		ans +=  dfs(i) ; 
	}
	  if(ans==(num/2)) 
		       printf("YES\n"); 
		   else      
		       printf("NO\n");
  }
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: