POJ1466-Girls and Boys

好純潔的題目呀!
求最大獨立集,用頂點數減去二分圖最大匹配即可。

#include <cstdio>
#include <cctype>
#include <vector>

const int maxv = 1000 + 5;

char s[maxv];

int n;
std::vector<int> G[maxv];
int match[maxv];
bool used[maxv];

void add_edge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
}

bool dfs(int v) {
    used[v] = true;
    for (int i = 0; i < G[v].size(); i++) {
        int u = G[v][i], w = match[u];
        if (w < 0 || (!used[w] && dfs(w))) {
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}

int Hungarian() {
    int res = 0;
    memset(match, -1, sizeof(match));
    for (int v = 0; v < n; v++) {
        if (match[v] < 0) {
            memset(used, 0, sizeof(used));
            if (dfs(v)) {
                res++;
            }
        }
    }
    return res;
}

int main() {
    while (scanf("%d", &n) == 1) {
        getchar();
        for (int i = 0; i < n; i++) {
            int v, m, u;
            scanf("%d: (%d)", &v, &m);
            for (int j = 0; j < m; j++) {
                scanf("%d", &u);
                add_edge(u, v);
            }
        }
        printf("%d\n", n - Hungarian());
        for (int i = 0; i < n; i++) {
            G[i].clear();
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章