【HDU4619】【二分匹配】【最大匹配】

来源:互联网 发布:程序员的量化交易之路 编辑:程序博客网 时间:2024/05/17 04:30

Warm up 2

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


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 30 00 30 11 11 34 50 10 23 12 20 01 02 04 13 20 0
 

Sample Output
46
 

Author
SYSU



#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;   int n,m;const int N = 1100;pair<int,int> p1[N],p2[N];vector<int> G[N];int match[N];int used[N];int uN;bool dfs(int u)  {      for(int i=0;i<G[u].size();i++)      {          if(!used[G[u][i]])          {              used[G[u][i]]=true;              if(match[G[u][i]]==-1||dfs(match[G[u][i]]))              {                  match[G[u][i]]=u;                  return true;              }          }      }      return false;  }  int hungary()  {      int u;      int res=0;      memset(match,-1,sizeof(match));      for(u=0;u<uN;u++)      {          memset(used,false,sizeof(used));          if(dfs(u)) res++;      }      return res;  }  int main(){    while(scanf("%d%d",&n,&m) != EOF){if(n == 0 && m == 0) break;for(int i=0;i<n;i++){G[i].clear();}memset(match,-1,sizeof(match));for(int i=0;i<n;i++){int x,y;scanf("%d%d",&x,&y);p1[i] = make_pair(x,y);}for(int j=0;j<m;j++){int x,y;scanf("%d%d",&x,&y);p2[j] = make_pair(x,y);}for(int i=0;i<n;i++){for(int j=0;j<m;j++){int x1 = p1[i].first;int y1 = p1[i].second;int x2 = p2[j].first;int y2 = p2[j].second;if((x1+1==x2 && y1==y2) || (x1+1==x2 && y1==y2+1) || (x1==x2 && y1==y2)|| (x1==x2 && y1==y2+1)){G[i].push_back(j);}}}//cout << "ok in 95 " << endl;uN = n;int ret = hungary();//cout << ret << endl;printf("%d\n",n+m-ret);}    return 0;}


0 0
原创粉丝点击