hdoj_1083/poj_1469_Courses 匹配匈牙利算法

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int ti, N, M, tp, tt;
bool use[310];
int g[110][310], from[310], tot;

bool match(int n){
    for(int i=1; i<=M; i++){
        if(!use[i] && g[n][i]){
            use[i] = 1;
            if(!from[i] || match(from[i])){
                from[i] =n;
                
                //cout<<"*"<<n<<" "<<i<<" "<<endl;
           
                return  true;
            }
        }
    }
    return false;
}

int hungary(){
    tot=0;
    for(int i=1; i<=N; i++){
        memset(use, 0, sizeof(use));
        if(match(i)){
            tot++;
        }
    }
    return tot;
}

int main(){
    cin>>ti;
    while(ti--){
        memset(g, 0, sizeof(g));
        memset(from, 0, sizeof(from));
        cin>>N>>M;
        for(int i=1; i<=N; i++){
            cin>>tp;
            for(int j=1; j<=tp; j++){
                cin>>tt;
                
                //g[tt][i]=true不用建雙向,更新會出錯
                g[i][tt]=true;
            }
        }
        int temp=hungary();
        //cout<<"hungary "<<temp<<endl;
        if(N==temp){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }
    //system("pause");
    return 0;
}

你可能感兴趣的:(hdoj_1083/poj_1469_Courses 匹配匈牙利算法)