zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    ELK学习实验004:Elasticsearch的简单介绍和操作
    ELK学习实验003:Elasticsearch 集群安装
    ELK学习实验002:Elasticsearch介绍及单机安装
    ELK学习实验001:Elastic Stack简介
    Eclipse 笔记
    自动
    Kali 无线网络
    安全和匿名
    Java 异常处理
    Java 构造结构私有化
  • 原文地址:https://www.cnblogs.com/moonbay/p/2640740.html
Copyright © 2011-2022 走看看