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;
}

HDU 4619 Warm up 2 最大独立集的更多相关文章

  1. hdu 4619 Warm up 2_最大独立集

    三个人整个下午都想不出这题 后来看题解,竟然用匈牙利算法的最大独立集,我顿时晕了. 题意:给竖着和横着的方块,除去重叠的,最多能留下几个方块 #include <cstdlib> #inc ...

  2. hdu 4619 Warm up 2(并查集)

    借用题解上的话,就是乱搞题.. 题意理解错了,其实是坐标系画错了,人家个坐标系,我给当矩阵画,真好反了.对于题目描述和数据不符的问题,果断相信数据了(这是有前车之鉴的hdu 4612 Warm up, ...

  3. hdu 4619 Warm up 2

    http://acm.hdu.edu.cn/showproblem.php?pid=4619 根据题意可知,每一个方格可能只被一个骨牌覆盖 可能被两个骨牌覆盖 也可能不被覆盖 有一个骨牌覆盖的方格(单 ...

  4. 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 S ...

  5. hdu 4619 Warm up 2 ( 二分图最大匹配 )

    题目:Warm up 2 题意:有横竖两种方式放着的多米诺骨牌,相同方向的不可能重叠,但是横放和竖放             的牌可能重叠.移走重叠的牌使剩下的牌最多. 分析:二分图匹配:最大独立集= ...

  6. hdu 4619 Warm up 2 (二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意: 平面上有一些1×2的骨牌,每张骨牌要么水平放置,要么竖直放置,并且保证同方向放置的骨牌不 ...

  7. hdu 4619 Warm up 2 网络流 最小割

    题意:告诉你一些骨牌,然后骨牌的位置与横竖,这样求最多保留多少无覆盖的方格. 这样的话有人用二分匹配,因为两个必定去掉一个,我用的是最小割,因为保证横着和竖着不连通即可. #include <s ...

  8. hdu 4619 Warm up 2 二分图匹配

    题目链接 给两种长方形, 水平的和垂直的, 大小都为1*2, n个水平的, m个垂直的, 给出它们的坐标. 水平的和垂直的可以相互覆盖, 但是同种类型的没有覆盖. 去掉一些长方形, 使得剩下的全部都没 ...

  9. HDU 4619 Warm up 2 贪心或者二分图匹配

    给同一张横着的牌的所在的格子编同一样的号,这些格子对应x集合,给同一张竖着的牌所在的格子编同一样的号,对应y集合,同一个格子上既有横着的牌又有竖着的牌,那么就建一条边,有冲突就要拿走一张,结果是总的牌 ...

随机推荐

  1. python碎片记录(三)

    1.不换行输出 for i in range(5):    print(i,end=' ')不换行打印,end表示每打印一个后面跟的字符 2.利用枚举方式打印输出索引与数值 a=[7,8,9]for ...

  2. [Leetcode] N-Queens 系列

    N-Queens 系列题解 题目来源: N-Queens N-Queens II N-Queens The n-queens puzzle is the problem of placing n qu ...

  3. RAID总结

    RAID-0: 这种模式若使用相同型号容量的磁盘来组成效果最佳.这种模式RAID会先将磁盘切出等量的区块chunk,当文件要存入RAID时先按照chunk的大小切割好,再依次存放到各个磁盘中去,由于磁 ...

  4. 137.Single Number II---位运算---《剑指offer》40

    题目链接:https://leetcode.com/problems/single-number-ii/description/ 题目大意:给出一串数,每个数都出现三次,只有一个数只出现一次,把这个出 ...

  5. NEERC2014

    NEERC2014 A - Alter Board 题目描述:给出一个\(n \times m\)的国际象棋棋盘,每次选定一个矩形,使得矩形中的每个格子的颜色翻转,求出最少次数的方案使得最终棋盘只有一 ...

  6. $FFT$(快速傅里叶变换)

    - 概念引入 - 点值表示 对于一个$n - 1$次多项式$A(x)$,可以通过确定$n$个点与值(即$x$和$y$)来表示这唯一的$A(x)$ - 复数 对于一元二次方程 $$x^2 + 1 = 0 ...

  7. linux系统磁盘挂载

    1.查看系统磁盘挂载情况 fdisk -l 2.格式化磁盘 mkfs -t ext3 /dev/sdb 3.挂在磁盘 mount /dev/sdb /disk2 4.查看磁盘挂载情况 df -h 5. ...

  8. [转载]FFmpeg完美入门[2] - FFmpeg参数说明

     1 通用选项 -L license -h 帮助 -fromats 显示可用的格式,编解码的,协议的. -f fmt 强迫采用格式fmt -i filename 输入文件 -y 覆盖输出文件 -t d ...

  9. HDU 2066 一个人的旅行(dijkstra水题+判重边)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题目大意:输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有 ...

  10. 20165301 2017-2018-2 《Java程序设计》第四周学习总结

    20165301 2017-2018-2 <Java程序设计>第四周学习总结 教材学习内容总结 第五章:子类与继承 一个类只能有一个父类,但是可以有若干个子类. 子类的继承性 子类和父类在 ...