hdu1281二分匹配关键边

棋盘游戏

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2024    Accepted Submission(s): 1172


Problem Description
小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击。
所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下。但是某些格子若不放子,就无法保证放尽量多的“车”,这样的格子被称做重要点。Gardon想让小希算出有多少个这样的重要点,你能解决这个问题么?
 

Input
输入包含多组数据,
第一行有三个数N、M、K(1<N,M<=100 1<K<=N*M),表示了棋盘的高、宽,以及可以放“车”的格子数目。接下来的K行描述了所有格子的信息:每行两个数X和Y,表示了这个格子在棋盘中的位置。
 

Output
对输入的每组数据,按照如下格式输出:
Board T have C important blanks for L chessmen.
 

Sample Input
  
  
3 3 4 1 2 1 3 2 1 2 2 3 3 4 1 2 1 3 2 1 3 2
 

Sample Output
  
  
Board 1 have 0 important blanks for 2 chessmen. Board 2 have 3 important blanks for 3 chessmen.
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
const int N = 200;
std::vector<int> mp[N];
int pre[N];
int flag[N];
int jumpi, jumpj;
int find(int cur)
{
    int k;
    for (int i = 0; i < mp[cur].size(); i++)
    {

        k = mp[cur][i];
        if (cur == jumpi && k == jumpj)
        {
            // flag[k]=true;
            continue;
        }
        if (!flag[k])
        {
            flag[k] = true;
            if (pre[k] == -1 || find(pre[k]))
            {
                pre[k] = cur;
                return 1;
            }
        }
    }
    return 0;
}
void clearmp()
{
    for (int i = 0; i < N; i++)
    {
        mp[i].clear();
    }
}
int cnt = 1;
int main()
{
#ifdef DeBUGs
    freopen("//home//amb//桌面//1.in", "r", stdin);
#endif
    int n, m;
    while (scanf("%d%d", &n, &m) + 1)
    {
        clearmp();
        int t;
        scanf("%d", &t);
        int a, b;
        int bian[10005][2] = zero;
        for (int i = 0; i < t; i++)
        {
            scanf("%d%d", &a, &b);
            mp[a].push_back(b);
            bian[i][0] = a;
            bian[i][1] = b;
        }
        jumpi = 0;
        jumpj = 0;
        memset(pre, -1, sizeof(pre));
        int sum = 0;
        for (int i = 1; i <= n; i++)
        {
            memset(flag, 0, sizeof(flag));
            if (find(i))
            {
                sum++;
                //printf("%d ", i);
            }
        }
        //   cout << sum << endl;
        int total = 0;
        for (int i = 0; i < t; i++)
        {
            jumpi = bian[i][0];//每次删除(在邻接表跳过)一条边,求最大匹配,若小于不删除的匹配,那么它为关键边
            jumpj = bian[i][1];
            memset(pre, -1, sizeof(pre));
            int sum2 = 0;
            for (int k = 1; k <= n; k++)
            {
                memset(flag, 0, sizeof(flag));
                if (find(k))
                {
                    sum2++;
                    //printf("%d ", i);
                }
            }
            //cout<<"sum2="<<sum2;
            if (sum2 < sum)
                total++;
        }
        printf("Board %d have %d important blanks for %d chessmen.\n", cnt++, total, sum);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值