hdu 4619 Warm up 2 (两种解法:1.并查集 2.最大独立集)

Warm up 2

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


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


题意:
有横竖两种方式放着的多米诺骨牌,相同方向的不可能重叠,但是横放和竖放的牌可能重叠。移走重叠的牌使剩下的牌最多。

思路1:
把牌重叠的方式多画几种,就能发现一个规律:能够放的牌数=(总的重叠个数+1)/2。这就可以用并查集了,只需要知道每个集合有多少个元素就够了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 105
#define maxx 2005
using namespace std;

int n,m,ans,cnt;
int pre[maxx],num[maxx],vis[maxx];
int h[maxn][maxn],v[maxn][maxn];

void init()
{
    int i,j;
    for(i=1;i<=n+m;i++)
    {
        pre[i]=i;
        num[i]=1;
    }
}
int find(int x)
{
    if(x==pre[x]) return x;
    pre[x]=find(pre[x]);
    return pre[x];
}
void merge(int x,int y)
{
    int i,j,u,v;
    u=find(x);
    v=find(y);
    if(u!=v)
    {
        pre[v]=u;
        num[u]+=num[v];
    }
}
void solve()
{
    int i,j,u;
    init();
    for(i=0;i<=100;i++)
    {
        for(j=0;j<=100;j++)
        {
            if(h[i][j]&&v[i][j])
            {
                merge(h[i][j],v[i][j]);
            }
        }
    }
    ans=0;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=n+m;i++)
    {
        u=find(i);
        if(!vis[u])
        {
            vis[u]=1;
            ans+=(num[u]+1)/2;
        }
    }
}
int main()
{
    int i,j,x,y;
    while(scanf("%d%d",&n,&m),n||m)
    {
        cnt=0;
        memset(h,0,sizeof(h));
        memset(v,0,sizeof(v));
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            h[x][y]=h[x+1][y]=++cnt;
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            v[x][y]=v[x][y+1]=++cnt;
        }
        solve();
        printf("%d\n",ans);
    }
    return 0;
}




思路2:
将横放的牌与竖放的牌分别看成两个点集,重叠的牌建边,则问题可以转化为求最大独立集的问题。
最大独立集 =顶点数 - 最大匹配数,所以就是一个二分图匹配问题了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn=2005;
const int maxm=10005;
const int INF=0x3f3f3f3f;
struct EDGE
{
    int b;
    int next;
};
int nx, ny, m,n;
EDGE edge[maxm];
int edge_num;
int first[maxn];
int cx[maxn],cy[maxn];       // cx[i]表示xi对应的匹配,cy[i]表示yi对应的匹配.
int distx[maxn],disty[maxn]; // 层的概念,即在BFS中的第几层.
int que[maxn];
int ans,cnt;
int v[105][105],h[105][105];
int mpv[105][105],mph[105][105];

inline void Init()
{
    memset(cx,-1,sizeof(cx));
    memset(cy,-1,sizeof(cy));
    memset(first,-1,sizeof(first));
    edge_num=0;
    ans=0;
}
inline void AddEdge(int a, int b)
{
    edge[edge_num].b=b;
    edge[edge_num].next=first[a];
    first[a]=edge_num++;
}
inline bool BFS()
{
    int i,j,k;
    bool flag(0);
    int h,t;
    memset(distx,0,sizeof(distx));
    memset(disty,0,sizeof(disty));
    h=t=0;
    for(i=1; i<=nx; ++i)
        if(cx[i]==-1)que[t++]=i;
    for(; h!=t; ++h)
    {
        i=que[h];
        for(k=first[i]; k!=-1; k=edge[k].next)
        {
            j=edge[k].b;
            if(!disty[j])
            {
                disty[j]=distx[i]+1;
                if(cy[j]==-1)flag=1;
                else distx[cy[j]]=disty[j]+1,que[t++]=cy[j];
            }
        }
    }
    return flag;
}
bool DFS(int i)
{
    int j,k;
    for (k=first[i]; k!=-1; k=edge[k].next)
    {
        j=edge[k].b;
        if(disty[j]==distx[i]+1)
        {
            // 说明j是i的后继结点.
            disty[j]=0; // j被用过了,不能再作为其他点的后继结点了.
            if(cy[j]==-1||DFS(cy[j]))
            {
                cx[i]=j,cy[j]=i;
                return 1;
            }
        }
    }
    return 0;
}
inline void Hopcroft_Karp()
{
    int i,j;
    while(BFS())
        for(i=1; i<=nx; ++i)
            if(cx[i]==-1 && DFS(i))++ans;
}
void solve()
{
    int i,j;
    for(i=0;i<=100;i++)
    {
        for(j=0;j<=100;j++)
        {
            if(mpv[i][j]&&mph[i][j])
            {
                AddEdge(v[i][j],h[i][j]);
            }
        }
    }
}
int main()
{
    int i, j;
    int a, b,tt,tmp;
    while(scanf("%d%d",&n,&m),n||m)
    {
        nx=n;
        ny=m;
        Init();
        cnt=0;
        memset(v,0,sizeof(v));
        memset(h,0,sizeof(h));
        memset(mpv,0,sizeof(mpv));
        memset(mph,0,sizeof(mph));
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&a,&b);
            v[a][b]=v[a+1][b]=++cnt;
            mpv[a][b]=mpv[a+1][b]=1;
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            h[a][b]=h[a][b+1]=++cnt;
            mph[a][b]=mph[a][b+1]=1;
        }
        solve();
        ans=0;
        Hopcroft_Karp();
        printf("%d\n", n+m-ans);
    }
    return 0;
}




 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值