HDU 4619 Warm up 2(2013多校2 1009 二分匹配)

Warm up 2

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


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
 

 

Recommend
zhuyuanchen520
 

 

 

 

 

 

 

相当于求最大独立集。

顶点数-二分匹配数

 

#include<stdio.h>

#include<iostream>

#include<algorithm>

#include<string.h>

#include<vector>

using namespace std;



//************************************************

const int MAXN=2505;//这个值要超过两边个数的较大者,因为有linker
int linker[MAXN];
bool used[MAXN];
vector<int>map[MAXN];
int uN;
bool dfs(int u)
{
    for(int i=0;i<map[u].size();i++)
    {
        if(!used[map[u][i]])
        {
            used[map[u][i]]=true;
            if(linker[map[u][i]]==-1||dfs(linker[map[u][i]]))
            {
                linker[map[u][i]]=u;
                return true;
            }
        }
    }
    return false;
}
int hungary()
{
    int u;
    int res=0;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++)
    {
        memset(used,false,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}
pair<int,int>p1[MAXN];
pair<int,int>p2[MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    int x,y;
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0 &&m==0)break;
        for(int i = 0;i < n;i++)
        {
            scanf("%d%d",&x,&y);
            p1[i]= make_pair(x,y);
        }
        for(int i = 0;i < m;i++)
        {
            scanf("%d%d",&x,&y);
            p2[i]= make_pair(x,y);
        }
        uN = n;
        for(int i = 0;i < n;i++)
            map[i].clear();
        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==x2 && y1==y2)
                   ||(x1==x2 && y1==y2+1)
                   ||(x1+1==x2 && y1==y2)
                   ||(x1+1==x2 && y1==y2+1)
                   )
                    map[i].push_back(j);
            }
        }
        int ans = n+m-hungary();
        printf("%d\n",ans);
    }
    return 0;
}

 

posted on 2013-07-25 17:34  kuangbin  阅读(871)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: