hdu1068最大独立集

一开始为建图纠结了半天,后来看了解题报告才突然明白,直接按输入建成的图,其最大匹配数为实际的图的2倍。。。

/*
 * hdu1068/win.cpp
 * Created on: 2012-8-15
 * 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 = 555;
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, size = mymap[k].size(); i < 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;
}
inline bool mypushback(vector<int> &v, int a) {
    for(int i = 0, size = v.size(); i < size; i++) {
        if(v[i] == a) {
            return false;
        }
    }
    v.push_back(a);
    return true;
}
bool buildgraph() {
    int t, k;
    if(scanf("%d", &N) == EOF) {
        return false;
    }
    init();
    for (int i = 0; i < N; i++) {
        scanf("%d: (%d)", &t, &k);
        for(int j = 0; j < k; j++) {
            scanf("%d", &t);
            mymap[i].push_back(t);
        }
    }
    return true;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    while(buildgraph()) {
        printf("%d\n", N - hungary() / 2);
    }
    return 0;
}
posted @ 2012-08-15 20:32  moonbay  阅读(152)  评论(0)    收藏  举报