首页 > 代码库 > poj1469

poj1469

匈牙利算法裸题

技术分享
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
const int N = 300;
int n, p, lin[N], cnt;
bool map[N][N],used[N];
bool search(int a)//寻找增广路 
{
    for(int j=1;j<=n;j++)
        if(map[a][j] && !used[j])
        {
            used[j] = 1;
            if(lin[j] == -1 || search(lin[j]))
            {
                lin[j] = a;
                return true;
            }
        }
    return false;
}
int main()
{
    int counti, temp,cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d %d",&p,&n);
        cnt = 0;
        memset(map, false, sizeof(map));
        memset(lin, -1, sizeof(lin));
        for(int i=1;i<=p;i++)
        {
            scanf("%d", &counti);
            for(int j=1;j<=counti;j++)
            {
                scanf("%d", &temp);
                map[i][temp] = true;
            }
        }
        for (int i=1;i<=p;i++)
        {
            memset(used, false, sizeof(used));
            if(search(i))cnt++;//每找到一条最大匹配就+1 
        }
        if(cnt==p)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
View Code

 

poj1469