Warm up 2

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

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.
 
 
思路:我们可以将这些多米诺骨牌看成点,若两个多米诺骨牌重合,则将它们两之间连一条边,建图之后,我们可以发现,这个图只可能由下列三种情况组成,一个单独的点,一条链,一个环,对于单独的点,说明没有其他骨牌和它重合,答案加1即可,对于一条链,我们可以发现这条链一定是由水平的骨牌和竖直的骨牌依次相连形成,这时我们可以通过将同方向的一种骨牌全部拿掉使得没有重合的骨牌(因为相同方向的骨牌不可能重合),这时设链的长度为S,则答案加 (S+1)/2即可,对于环的情况与链的情况相同。;另外二分图最大匹配也可以做。
下面是参考代码:
 
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#define maxn 110
using namespace std;
int map[maxn][maxn];
int vis[2010];
struct edge
{
int to;
int next;
}e[20010];
int box[2010],cnt;
void init()
{
cnt=0;
memset(box,-1,sizeof(box));
}
void add(int from,int to)
{
e[cnt].to=to;
e[cnt].next=box[from];
box[from]=cnt++;
}
int dfs(int now)
{
vis[now]=1;
int num=1;
for(int t=box[now];t+1;t=e[t].next)
{
int v=e[t].to;
if(!vis[v])
num+=dfs(v);
}
return num;
}
int main()
{
//freopen("dd.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m)&&(m+n))
{
int i,x,y;
init();
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
map[x][y]=map[x+1][y]=i;
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(map[x][y])
{
add(map[x][y],i+n);
add(i+n,map[x][y]);
}
if(map[x][y+1])
{
add(map[x][y+1],i+n);
add(i+n,map[x][y+1]);
}
}
int ans=0;
memset(vis,0,sizeof(vis));
for(i=1;i<=n+m;i++)
{
if(!vis[i])
{
int tmp=dfs(i);
ans+=(tmp+1)/2;
}
}
printf("%d\n",ans);
}
return 0;
}

2013多校联合2 I Warm up 2(hdu 4619)的更多相关文章

  1. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  2. HDU 多校联合练习赛2 Warm up 2 二分图匹配

    Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  3. 2013 多校联合2 D Vases and Flowers (hdu 4614)

    Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others ...

  4. 2013 多校联合 2 A Balls Rearrangement (hdu 4611)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  5. 2013多校联合3 G The Unsolvable Problem(hdu 4627)

    2013-07-30 20:35 388人阅读 评论(0) 收藏 举报 http://acm.hdu.edu.cn/showproblem.php?pid=4627 The Unsolvable Pr ...

  6. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  7. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  8. 2016暑假多校联合---Substring(后缀数组)

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  9. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

随机推荐

  1. 【转】Unable to execute dex: Java heap space 解决方案(如何为eclipse.int 添加内存)

    原文网址:http://blog.csdn.net/zengyangtech/article/details/7003379 欢迎转载,转载请注明 http://blog.csdn.net/zengy ...

  2. Letter Combinations of a Phone Number——LeetCode

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  3. 实现zbar扫描二维码的时候就把照片存储出来的办法

    业务场景       当实现二维码扫描的时候(用的扫码库是zbar),有时候需要悄悄的整个扫描的照片存储下来,(charles有部分变态业务就是这样)就是说给扫描的图片照个全景照片. 那么代码如何实现 ...

  4. C++ 匿名名字空间及静态非成员函数

    在C++中,static有一个感觉被较少提及的用法:修饰非成员函数,这个用法实际是从C语言继承来的.其作用是表明这个函数只在当前编译单元中有效.这就使这个函数的所有引用在编译时就可以全部确定,无需进入 ...

  5. php显示日期(今天、昨天、本周、上周、本月、上月、)

    <?php //今天 $today = date("Y-m-d"); //昨天 $yesterday = date("Y-m-d", strtotime( ...

  6. 1st day

    学习配置环境,听的一脸懵逼,不过还好装好了,哈哈哈... <?php /*写一个函数,该函数可以将给定的任意个数的参数以指定的字符串串接起来成为一个长的字符串.该函数带2个或2个以上参数,其中第 ...

  7. GDB错误:Cannot find bounds of current function

    http://blog.csdn.net/zoomdy/article/details/17249165 mingdu.zheng <at> gmail <dot> com 使 ...

  8. SKCropNode类

    继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode) NSCopying(SKNode) NSObject(NSObject) 框架 /System/ ...

  9. UIKit和Core Graphics绘图(一)——字符串,线条,矩形,渐变

    概述 CoreGraphics也称为Quartz 2D 是UIKit下的主要绘图系统,频繁的用于绘制自定义视图.Core Graphics是高度集成于UIView和其他UIKit部分的.Core Gr ...

  10. [Redux] Writing a Todo List Reducer (Adding a Todo)

    Learn how to implement adding a todo in a todo list application reducer. let todo = (state = [], act ...