D - Warm up 2

来源:互联网 发布:macbook下载软件工具 编辑:程序博客网 时间:2024/05/09 12:58
D - Warm up 2
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status
Appoint description: 

Description

  Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizontally or vertically. It's guaranteed the dominoes in the same direction are not overlapped, but horizontal and vertical dominoes may overlap with each other. You task is to remove some dominoes, so that the remaining dominoes do not overlap with each other. Now, tell me the maximum number of dominoes left on the board.
 

Input

  There are multiple input cases. 
  The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes. 
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y). 
  Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1). 
  Input ends with n = 0 and m = 0.
 

Output

  For each test case, output the maximum number of remaining dominoes in a line.
 

Sample Input

2 30 00 30 11 11 34 50 10 23 12 20 01 02 04 13 20 0
 

Sample Output

46
 
这个是看了别人解题报告写的,说是用了二分图求最大匹配,然后就去各种查资料,学了快4个小时稍微明白点什么意思了,就根据理解再写了一遍,这个题还是挺纯的一个二分图的求最大匹配,我照着百度上的匈牙利算法写了一遍就AC了,今天还是挺高兴,算是学习了。
#include<stdio.h>#include<stdlib.h>#include<string.h>int n,m;int g[1005][1005],vis[1005],back[1005];struct coor{    int x;    int y;}h[1005],v[1005];int judge(int a,int b){    if(h[a].x+1==v[b].x||h[a].x==v[b].x)        if(h[a].y==v[b].y||h[a].y==v[b].y+1)return 1;    return 0;}bool find(int a){       for(int i=1;i<=m;i++){           if(g[a][i]&&vis[i]==0){//如果节点i与a相邻且并未查找过                vis[i]=1;//表示已经查找过               if(back[i]==0||find(back[i])){//如果i为一个未覆盖的点或者已经是以个匹配点但从其相邻点a出发可以                      back[i]=a;              //找到增广路,那么就返回真表示已经找到增广路,同时标记好i是从哪个点照过来的                      return true;                }            }       }            return false;}int main(){    int ans;    while(scanf("%d %d",&n,&m)==2&&(n||m)){        for(int i=1;i<=n;i++)            scanf("%d %d",&h[i].x,&h[i].y);        for(int i=1;i<=m;i++)            scanf("%d %d",&v[i].x,&v[i].y);        memset(g,0,sizeof(g));        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)if(judge(i,j)){                g[i][j]=1;                   }        memset(back,0,sizeof(back));        ans=0;        for(int i=1;i<=n;i++){             memset(vis,0,sizeof(vis));             if(find(i))ans++;        }        printf("%d\n",n+m-ans);    }    return 0;}