HDU 4619 Warm up 2 最大独立集

Warm up 2

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=4619

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

Hint

题意

现在有1*2的纸牌

有n个是竖着放置的,有m个数平行放置的

他们可能是压在一起的,现在让你拿起来一些,使得纸牌互相不重叠

问你平面上最多还剩下多少个

题解:

把压在一起的建一条边,显然答案就是最大独立点集

代码

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e3 + 15;
pair < int , int > p[maxn];
int n , m , mat[105][105] , Left[maxn] , vis[maxn];
vector < int > e[maxn];

int dfs(int x){
    for(auto it : e[x]){
        if(Left[it] == -1){
            Left[it] = x;
            return 1;
        }
        if(vis[it]) continue;
        vis[it] = 1;
        if(dfs(Left[it])){
            Left[it] = x;
            return 1;
        }
    }
    return 0;
}

int main(int argc,char *argv[]){
    while(scanf("%d%d",&n,&m)){
        if( n == m && n == 0 ) break;
        memset( mat , 0 , sizeof( mat ) );
        for(int i = 1 ; i <= n ; ++ i){
            int x , y ;
            scanf("%d%d",&x,&y);
            p[i].first = x , p[i].second = y;
        }
        for(int i = 1 ; i <= m ; ++ i){
            int x , y ;
            scanf("%d%d" , &x , & y);
            mat[x][y] = mat[x][y+1] = i;
        }
        memset( Left , -1 , sizeof( Left ) );
        for(int i = 1 ; i <= n ; ++ i){
            e[i].clear();
            int x = p[i].first;
            int y = p[i].second;
            if(mat[x][y]) e[i].push_back(mat[x][y]);
            if(mat[x+1][y]) e[i].push_back(mat[x+1][y]);
        }
        int match = 0;
        for(int i = 1 ; i <= n ; ++ i){
            memset( vis , 0 , sizeof( vis ) );
            match += dfs( i );
        }
        printf("%d\n" , n + m - match );
    }
    return 0;
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5373607.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞