ZCMU 1902: Rhombinoes ( 二分图匹配)

1902: Rhombinoes

Description

Rhombinoes

In the game of Rhombinoes, you have a board made up entirely of equilateral trianges (see the image), some of which are "live" and some are "dead". Your goal is to place down as many rhombinoes ("rhombus"-shaped pieces) as possible on the board. Each rhombino should exactly cover two "adjacent" live triangles that have a common side, and no two rhombinoes can use the same triangle.

Given the description of the live and dead triangles of a Rhombino board, whssadat is the maximum number of rhombinoes you can simultaneously place down on the board?

Description of Board

Each triangle in the board has a pair of coordinates (x, y). The bottom-left triangle has coordinates (0, 0) and will always be a triangle with its tip pointed upward. For any given triangle with coordinates (x, y), the triangle adjacent to it on its right-side (if any) has coordinates (x+1, y), and the triangle adjacent to it on its top-side (if any) has coordinates (x, y+1). Left-side and bottom-side adjacency are defined similarly.

Each board has a width W and a height H. A board with width W and height H is the board which consists of all triangles with coordinates (x, y) such that 0 ≤ x < W and 0 ≤ y < H. For example, the game board in the image has width 6 and height 3.

Input

The first line of input contains three space-separated integers WH, and K.

W is the width of the board, H is the height, and K is the number of dead triangles on the board (1 ≤ W ≤ 1001 ≤ H ≤ 1001 ≤ K ≤ W*H ≤ 1000).

Exactly K lines will follow. Each such line will contain a pair of space-separated integers x and y (0 ≤ x < W, 0 ≤ y < H), indicating that the triangle with coordinates (x,y) is a dead triangle. All other triangles are live.

Output

Output a line containing a single integer, the maximum number of rhombinoes you can simultaneously place down on the board.

Sample Input

6 3 4
1 1
2 2
4 1
3 0

Sample Output

5

HINT


This is the board in the image, with cells (1, 1), (2, 2), (4, 1), and (3, 0) dead.


题意  W,H,K分别表示长 ,高  和 死三角形的个数(不能放rhombinoes的地方)( rhombinoes就是两个三角形连接形成的图形) 接下来K行表示死三角形的坐标。求最多能放多少个 rhombinoes?

图中有两种三角形 :一种是“朝上的三角形”,一种是“朝下的三角形”。可以发现与“朝上的三角形”连接的都是“朝下的三角形”,反之亦然。所以我们可以把它们分成两组,朝上的三角形和朝下的三角形。一个集合中每个节点只与另一个集合中的一个相连。这就变成了 二分图匹配的问题了 我是用复杂度为O(mn)的匈牙利算法做的。用0~W*H 表示每个三角形。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<memory.h>
using namespace std;
#define ll long long
#define cl(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
#define INF 999999999
vector<int>g[1005];
int match[1005];
bool vis[1005];
bool dead[105][105];
bool dfs(int i)
{
    if(vis[i])return false;
    vis[i]=true;
    int n=g[i].size();
    for(int x=0;x<n;++x) {
        int j=g[i][x];
        if(match[j]==-1||dfs(match[j]))
        {
            match[i]=j;
            match[j]=i;
            return true;
        }
    }
    return false;
}
int maxmatch(int N)
{
    cl(match,-1);
    int ans=0;
    for(int i=0;i<N;++i)
        if(match[i]==-1)
        {
            cl(vis,0);
            if(dfs(i))ans++;
        }
    return ans;
}
int main()
{
    int W,H,K,x,y;
    while(~scanf("%d%d%d",&W,&H,&K))
    {
       cl(dead,false);
        for(int i=0;i<K;i++)
        {
            scanf("%d%d",&x,&y);
            dead[x][y]=true;
        }
        int x2,y2;
        for(int i=0;i<W;i++)
            for(int j=0;j<H;j++)
            {
                if(dead[i][j])continue;
                if(((i+j)%2==1)&&j<H-1)
                {
                    x=i;y=j+1;
                    if(!dead[x][y])
                    {
                        g[j*W+i].push_back(y*W+x);
                        g[y*W+x].push_back(j*W+i);
                    }
                }
                if(i<W-1)
                {
                    x=i+1;y=j;
                    if(!dead[x][y])
                    {
                        g[j*W+i].push_back(y*W+x);
                        g[y*W+x].push_back(j*W+i);
                    }
                }
            }
        int ans=maxmatch(W*H);
        printf("%d\n", ans);
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值