HDU 4619 Warm up 2 最大独立集

探讨了如何通过算法解决二分图中寻找最大数量不重叠1*2多米诺骨牌的问题,并提供了一个具体的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Warm up 2

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


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
 

Author
SYSU
 

Source


给出坐标系上一些点,在里面放着一些1*2的多米诺骨牌。

现在从上面取走一些方块,使摆放不能重合。最多可以保留多少互不重合的方块。

二分图最大独立集。


/** Author: ☆·aosaki(*’(OO)’*)  niconiconi★ **/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <stack>
//#include <tuple>
#define ALL(v) (v).begin(),(v).end()
#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)
#define SIZE(v) ((int)(v).size())
#define mem(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lp(k,a) for(int k=1;k<=a;k++)
#define lp0(k,a) for(int k=0;k<a;k++)
#define lpn(k,n,a) for(int k=n;k<=a;k++)
#define lpd(k,n,a) for(int k=n;k>=a;k--)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define lowbit(x) (x&(-x))
#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define PI acos(-1.0)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define TT cout<<"*****"<<endl;
#define TTT cout<<"********"<<endl;
inline int gcd(int a,int b)
{
    return a==0?b:gcd(b%a,a);
}

#define MAXN 2555
using namespace std;

int link[MAXN];
bool used[MAXN];
vector<int> vec[MAXN];
int n,m;

bool dfs(int u)
{
   lp0(i,vec[u].size())
    {
        if(!used[vec[u][i]])
        {
            used[vec[u][i]]=1;
            if(link[vec[u][i]]==-1 || dfs(link[vec[u][i]]))
            {
                link[vec[u][i]]=u;
                return 1;
            }
        }
    }
    return 0;
}

int hun()
{
    int u;
    int re=0;
    mem1(link);
    lp0(u,n)
    {
       mem(used);
        if(dfs(u)) re++;
    }
    return re;
}

struct aoi1
{
    int x,y;
}p1[2010];

struct aoi2
{
    int x,y;
}p2[2010];

int main()
{
    //freopen("in.txt","r",stdin);
    int x,y;
    while(~sc2(n,m))
    {
        if(n==0&&m==0)break;
        lp0(i,n)
        {
            sc2(p1[i].x,p1[i].y);
        }
        lp0(i,m)
        {
            sc2(p2[i].x,p2[i].y);
        }
        lp0(i,n) vec[i].clear();
        lp0(i,n)
        {
           lp0(j,m)
            {
                int x1=p1[i].x;
                int y1=p1[i].y;
                int x2=p2[j].x;
                int y2=p2[j].y;
                if( (x1==x2 && y1==y2)||(x1==x2 && y1==y2+1)||(x1+1==x2 && y1==y2)||(x1+1==x2 && y1==y2+1) )
                    vec[i].pb(j);
            }
        }
        printf("%d\n",n+m-hun());
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值