码迷,mamicode.com
首页 > 编程语言 > 详细

HD1083 二分图,匈牙利算法

时间:2015-08-31 13:09:29      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define N 500
int n1, n2, ans;
int result[N];
bool state[N];
bool map[N][N];

bool find(int x)
{
    for (int i = 1; i <= n2; ++i){
        if (map[x][i] && !state[i]){
            state[i] = true;
            if (result[i] == 0 || find(result[i])){
                result[i] = x;
                return true;
            }
        }
    }
    return false;
}
int hungary()
{
    for (int i = 1; i <= n1; ++i){
        memset(state, 0, sizeof(state));
        if (find(i)) ++ans;
    }
    return ans;
}
int main()
{
    int t, p, n;
    int a, b;
    scanf("%d", &t);
    while (t--){
        memset(map, 0, sizeof(map));
        memset(result, 0, sizeof(result));
        ans = 0;
        scanf("%d %d", &p, &n);
        for (int i = 1; i <= p; ++i){
            scanf("%d", &a);
            for (int j = 1; j <= a; ++j){
                scanf("%d", &b);
                map[i][b] = true;
            }
        }
        n1 = p; n2 = n;
        //cout << hungary() << endl;
        if (p == hungary()) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}

 

HD1083 二分图,匈牙利算法

标签:

原文地址:http://www.cnblogs.com/jokoz/p/4772698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!