2013多校联合2 I Warm up 2(hdu 4619)
2013-07-25 21:38
393 查看
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
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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)
- hdu 4619 warm up 2 并查集或搜索都可以做出来的题 2013多校联合训练第二场
- hdu 2013 ACM/ICPC Asia Regional Online —— Warmup解题报告
- HDU 4666 Hyperspace(2013多校联合7 1001)
- HDU 4669 Mutiples on a circle (2013多校联合7 1004)
- 2013多校联合8 1004 Terrorist’s destroy(hdu 4679)
- HDU-4619:Warm up 2(二分图最大独立集)
- hdu 4697 Convex hull 对移动的凸包积分 利用叉积的分配率 (2013多校联合)
- 2013 多校联合 C Partition (hdu 4602)
- hdu 4674 边双连通缩点+倍增lca+麻烦的讨论 (2013多校联合)
- 2013 多校联合4 1011 Fliping game (hdu 4642)
- HDU 4643 GSM 简单计算几何 (2013多校联合)
- HDU 4651 2013多校联合第5场 Partition 数论
- hdu 4607 park visit 2013多校联合训练第一场
- HDU 4637 Rain on your Fat brother 线段与半圆和线段交 简单题 (2013多校联合)
- 2013 多校联合2 D Vases and Flowers (hdu 4614)
- 2013 多校联合 2 A Balls Rearrangement (hdu 4611)
- 2013 多校第二场 hdu 4619 Warm up 2
- hdu 4677 并查集+分块算法 好题 (2013多校联合)
- hdu 4661 Message Passing 树形dp (2013多校联合)