HDU 4619 Warm up 2

                       Warm up 2

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 342    Accepted Submission(s): 165


Problem 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 3 0 0 0 3 0 1 1 1 1 3 4 5 0 1 0 2 3 1 2 2 0 0 1 0 2 0 4 1 3 2 0 0
 
Sample Output
4 6
 
Source
题意分析: 水平N牌 ,垂直M牌 ,水平相互独立(即不想交) ,垂直也是如此。 
          可以看出这是一个二分图。
          但是水平的牌和垂直的会相交,求最少踢出去几张牌,使得剩下的牌都不相交 。
          那么把水平和垂直相交的牌连边,求出最大匹配数,把这些匹配的边切断,剩下的牌就相互不交了。也就是最大独立点。 代码写漂亮点,即使拿不到奖,对找工作有帮助的,同学们。
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <set>
#include <limits.h>
#define Max(a,b) ((a)>(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int size=1208 ;
struct Me{
   int N ,M ;
   bool grid[size][size] ;
   int match[size] ;
   int used[size] ;
   Me(){};
   Me(int n ,int m):N(n),M(m){};
   struct Node{
        int x1 ,y1 ;
        int x2 ,y2 ;
   }horizontal[size] ,vertical[size];
   void read(){
      for(int i=1;i<=N;i++){
           scanf("%d%d",&horizontal[i].x1,&horizontal[i].y1) ;
           horizontal[i].x2=horizontal[i].x1+1 ;
           horizontal[i].y2=horizontal[i].y1 ;
      }
      for(int i=1;i<=M;i++){
           scanf("%d%d",&vertical[i].x1,&vertical[i].y1) ;
           vertical[i].x2=vertical[i].x1 ;
           vertical[i].y2=vertical[i].y1+1 ;
      }
   }
   int is_ok(int i, int j){
      if(horizontal[i].x1==vertical[j].x1&&horizontal[i].y1==vertical[j].y1)
          return 1 ;
      if(horizontal[i].x1==vertical[j].x2&&horizontal[i].y1==vertical[j].y2)
          return 1 ;
      if(horizontal[i].x2==vertical[j].x1&&horizontal[i].y2==vertical[j].y1)
          return 1 ;
      if(horizontal[i].x2==vertical[j].x2&&horizontal[i].y2==vertical[j].y2)
          return 1 ;
      return 0 ;
   }
   void make_grap(){
      memset(grid,0,sizeof(grid)) ;
      for(int i=1;i<=N;i++)
        for(int j=1;j<=M;j++)
           grid[i][j]=is_ok(i,j) ;
   }
   int dfs(int u){
       for(int v=1;v<=M;v++){
          if(grid[u][v]&&!used[v]){
             used[v]=1 ;
             if(match[v]==-1||dfs(match[v])){
                  match[v]=u ;
                  return 1 ;
             }
          }
       }
       return 0 ;
   }
   int max_match(){
      int ans=0 ;
      memset(match,-1,sizeof(match)) ;
      for(int i=1;i<=N;i++){
         memset(used,0,sizeof(used)) ;
         if(dfs(i))
            ans++ ;
      }
      return ans ;
   }
   int gao_qi(){
       read() ;
       make_grap() ;
       return N+M-max_match() ;
   }
};
int main(){
   int n ,m ;
   while(cin>>n>>m){
       if(n==0&&m==0)
          break  ;
       Me me(n,m) ;
       cout<<me.gao_qi()<<endl ;
   }
   return 0 ;
}
 
原文地址:https://www.cnblogs.com/liyangtianmen/p/3216329.html