hdu1083完备匹配的判定

直接上代码。

/*
 * hdu1083/win.cpp
 * Created on: 2012-8-16
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MAXN = 305;
vector<int> mymap[MAXN];
int N, M, mymatch[MAXN];
bool visited[MAXN];
void init() {
    for(int i = 0; i < N; i++) {
        mymap[i].clear();
    }
}
bool dfs(int k) {
    int t, I;
    for(int i = 0; i < (int)mymap[k].size(); i++) {
        I = mymap[k][i];
        if(!visited[I]) {
            visited[I] = true;
            t = mymatch[I];
            mymatch[I] = k;
            if(t == -1 || dfs(t)) {
                return true;
            }
            mymatch[I] = t;
        }
    }
    return false;
}
int hungary () {
    memset(mymatch, -1, sizeof(mymatch));
    int ans = 0;
    for (int i = 0; i < N; i++) {
        memset(visited, false, sizeof(visited));
        if (dfs(i)) {
            ans++;
        }
    }
    return ans;
}
bool buildgraph() {
    int t, K;
    init();
    for (int i = 0; i < N; i++) {
        scanf("%d", &K);
        for(int k = 0; k < K; k++) {
            scanf("%d", &t);
            mymap[i].push_back(t - 1);
        }
    }
    return true;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    int T, P;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &N, &P);
        buildgraph();
        puts(hungary() == N ? "YES" : "NO");
    }
    return 0;
}
posted @ 2012-08-16 11:44  moonbay  阅读(1311)  评论(0编辑  收藏  举报