码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4619 Warm up 2 (二分图)

时间:2014-08-01 19:58:22      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   strong   

Warm up 2

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


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

题意:一个多米诺骨牌占两个格子,有的占水平的两个格子,有的占据垂直的两个格子,这些多米诺骨牌有的重叠。现在需要移去相互重叠的,求最多留下几个多米诺骨牌。

思路:一个多米诺骨牌占两个格子连一条边,求最大匹配OK!

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define N 105
#define M 4005
int mark[M],link[M],g[N][N];
int n,m,x,y,t;
int head[M];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
struct node
{
    int u,v,next;
}map[M];
int max(int a,int b)
{
    return a>b?a:b;
}
void add(int u,int v)
{
    map[t].u=u;
    map[t].v=v;
    map[t].next=head[u];
    head[u]=t++;
}
int find(int k)
{
    int i,v;
    for(i=head[k];i!=-1;i=map[i].next)
    {
        v=map[i].v;
        printf("%d ",v);
        if(!mark[v])
        {
            mark[v]=1;
            if(link[v]==-1||find(link[v]))
            {
                link[v]=k;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,u,v,cnt;
    while(scanf("%d%d",&n,&m),n||m)
    {
        memset(g,-1,sizeof(g));
        memset(map,0,sizeof(map));
        memset(head,-1,sizeof(head));
        t=x=y=0;
        cnt=0;
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            x=max(x,u+1);y=max(y,v);
            if(g[u][v]==-1)
            {
                g[u][v]=cnt++;
            }
            if(g[u+1][v]==-1)
            {
                g[u+1][v]=cnt++;
            }
            add(g[u][v],g[u+1][v]);
            add(g[u+1][v],g[u][v]);
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            x=max(x,u);y=max(y,v+1);
            if(g[u][v]==-1)
            {
                g[u][v]=cnt++;
            }
            if(g[u][v+1]==-1)
            {
                g[u][v+1]=cnt++;
            }
            add(g[u][v],g[u][v+1]);
            add(g[u][v],g[u][v+1]);
        }
        x++;y++;
        int ans=0;
        for(i=0;i<cnt;i++)
        {
            memset(mark,0,sizeof(mark));
            printf("%d :",i);
            ans+=find(i);
            puts("");
        }
        printf("%d\n",ans/2);
    }
    return 0;
}


hdu 4619 Warm up 2 (二分图),布布扣,bubuko.com

hdu 4619 Warm up 2 (二分图)

标签:des   style   blog   http   color   java   os   strong   

原文地址:http://blog.csdn.net/u011721440/article/details/38338183

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!