天天看點

HDU 1274 匈牙利算法

裸題

附一個二分圖學習連結

二分圖相關知識指南

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#define N 310
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
bool line[N][N];
bool used[N];
int match[N];
bool search(int x){
    for(int i = 1;i<= m;i++){
        if(!used[i] && line[x][i]){
            used[i] = 1;
            if(!match[i] || search(match[i])){
                match[i] = x;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int i,j,k,x,y;
    while (~scanf("%d",&n))
    {
        memset(line,0,sizeof(line));
        memset(match,0,sizeof(match));
        scanf("%d",&m);
        for(i=1;i<=n;i++){
            scanf("%d",&k);
            for(j=1;j<=k;j++){
                scanf("%d",&x);
                line[i][x] = 1;
            }
        }
        int sum = 0;
        for(i=1;i<=n;i++){
            memset(used,0,sizeof(used));
            if(search(i)){
                sum++;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}