您的位置:首页 > Web前端

POJ-1274-The Perfect Stall 解题报告

2014-11-12 15:47 417 查看
求二分图最大匹配。题意:农夫的牛栏上个星期刚刚完成。但是因为工程设计问题,所有的牛栏都不一样。第一个星期,农夫为他的牛随意分配了牛栏。但是他很快地意识到有一些牛只在指定的牛栏中才会产奶。他想尽可能多的使牛能够产奶,但是每一头牛只能分配给一个牛栏且每一个牛栏也只能被一头牛分配到。请你计算出最多能分配多少对使得牛产奶最多。

我的解题思路:这是赤裸裸的求二分图最大匹配,不用多说。

我的解题代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

#define N 202

int match
;
bool vis
;
vector <int> e
;
int n, m;

void InitRead();

void DataProcess();

bool Dfs(int x);

int main()
{
    while (~scanf("%d %d", &n, &m))
    {
        InitRead();
        DataProcess();
    }
    return 0;
}

void InitRead()
{
    memset(match, -1, sizeof(match));
    int a, b;
    for (int i=1; i<=n; ++i)
    {
        e[i].clear();
        scanf("%d", &a);
        while (a--)
        {
            scanf("%d", &b);
            e[i].push_back(b);
        }
    }
    return;
}

void DataProcess()
{
    int ans = 0;
    for (int i=1; i<=n; ++i)
    {
        memset(vis, false, sizeof(vis));
        if (Dfs(i)) ans++;
    }
    printf("%d\n", ans);
    return;
}

bool Dfs(int x)
{
    int size = e[x].size();
    for (int i=0; i<size; ++i)
    {
        if (!vis[e[x][i]])
        {
            vis[e[x][i]] = true;
            if (match[e[x][i]] == -1 || Dfs(match[e[x][i]]))
            {
                match[e[x][i]] = x;
                return true;
            }
        }
    }
    return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: