http://acm.hdu.edu.cn/showproblem.php?pid=4619
Warm up 2
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 426 Accepted Submission(s): 213
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
2013 Multi-University Training Contest 2
Recommend
zhuyuanchen520
解析:
有1*2的牌将其横放和竖放,且横放之间胡不重叠,竖放的之间也不互不重叠,但横竖放的可能会发生重叠,问拿掉一些牌使得剩下的牌都互不重叠,求剩下的最大牌数
思路:
二分匹配;
将横放与竖放牌根据是否有重叠部分,进行建图。
然后求二分匹配求最大匹配
最后结果就是总的牌数减去所得的最大匹配数
注:覆盖情况如下:
2013-07-26 12:03:14 Accepted 15MS 4284K 1219 B C++
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=1000+10;
int map[maxn][maxn];
int vis[maxn],result[maxn];
int n,m;
struct line
{
int y;
int x;
}p1[maxn],p2[maxn];
bool find(int a)
{
for(int i=1;i<=m;i++)
{
if(map[a][i]==1&&!vis[i])
{
vis[i]=1;
if(result[i]==0||find(result[i]))
{
result[i]=a;
return true;
}
}
}
return false;
}
bool connect(line a1,line a2)//判断两张牌是否有重叠部分
{
if((a1.x==a2.x)&&(a1.y==a2.y||a1.y==a2.y+1))
{
return true;
}
if((a1.x+1==a2.x)&&(a1.y==a2.y||a1.y==a2.y+1))
{
return true;
}
return false;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(i=1;i<=n;i++)
{
scanf("%d%d",&p1[i].x,&p1[i].y);
}
for(j=1;j<=m;j++)
scanf("%d%d",&p2[j].x,&p2[j].y);
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(connect(p1[i],p2[j]))//将有重叠的两张牌建图
map[i][j]=1;
int ans=0;
memset(result,0,sizeof(result));
for(i=1;i<=n;i++)//寻找最大匹配数
{
memset(vis,0,sizeof(vis));
if(find(i))
ans++;
}
//printf("%d\n",ans);
printf("%d\n",n+m-ans);
}
return 0;
}