POj---1469---Courses

题目链接:http://poj.org/problem?id=1469   http://acm.hdu.edu.cn/showproblem.php?pid=1083

题意:

有n个学生p门课程, 每门课程有cnt个人可以选择

1.每个人最后都有单独对应不同于任何人的一门课程;

2.每门课都有人选;

满足以上两个条件输出YES,否则NO;

本质就是求最大匹配是否等于p;

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<algorithm>
using namespace std;

const int maxn=550;
const int INF=0x3f3f3f3f;

int G[maxn][maxn];
int used[maxn];
int vis[maxn];
int p, n;

///匈牙利算法
bool Hungary(int u)
{
    for(int i=1; i<=p; i++)
    {
        if(!vis[i]&&G[u][i])
        {
            vis[i]=1;
            if(!used[i] || Hungary(used[i]))
            {
                used[i]=u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int T;
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d %d", &p, &n);

        memset(G, 0, sizeof(G));
        for(int i=1; i<=p; i++)
        {
            int cnt, x;
            scanf("%d", &cnt);
            while(cnt--)
            {
                scanf("%d", &x);
                G[x][i]=1;
            }
        }

        memset(used, 0, sizeof(used));
        int ans=0;

        for(int i=1; i<=n; i++)
        {
            memset(vis, 0, sizeof(vis));
            if(Hungary(i))
                ans++;
        }

        if(ans==p)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/w-y-1/p/5741815.html