zoukankan      html  css  js  c++  java
  • HDU1068 (二分图最大匹配匈牙利算法)

    Girls and Boys

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 9100    Accepted Submission(s): 4185


    Problem Description
    the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set.

    The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

    the number of students
    the description of each student, in the following format
    student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
    or
    student_identifier:(0)

    The student_identifier is an integer number between 0 and n-1, for n subjects.
    For each given data set, the program should write to standard output a line containing the result.
     
    Sample Input
    7
    0: (3) 4 5 6
    1: (2) 4 6
    2: (0)
    3: (0)
    4: (2) 0 1
    5: (1) 0
    6: (2) 0 1
    3
    0: (2) 1 2
    1: (1) 0
    2: (1) 0
     
    Sample Output
    5
    2
     
    Source
     

    这题大概意思就是说找出一个最大的集合使得该集合的任意两个人木有关系。

    根据最大独立集 =顶点数 - 最大匹配数

    由于题目没有给出哪些是男的哪些是女的,也就是说没有明显的二分图,所以将一个人拆成两个人进行最大匹配。由于一个拆成两个,所以最大匹配数应该是求出来的数除以2 。最后再用顶点数减就行了。

    匈牙利算法求二分图最大匹配

    /*
    ID: LinKArftc
    PROG: 1068.cpp
    LANG: C++
    */
    
    #include <map>
    #include <set>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <cstdio>
    #include <string>
    #include <utility>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define eps 1e-8
    #define randin srand((unsigned int)time(NULL))
    #define input freopen("input.txt","r",stdin)
    #define debug(s) cout << "s = " << s << endl;
    #define outstars cout << "*************" << endl;
    const double PI = acos(-1.0);
    const double e = exp(1.0);
    const int inf = 0x3f3f3f3f;
    const int INF = 0x7fffffff;
    typedef long long ll;
    
    const int maxn = 1000;
    
    int mp[maxn][maxn];
    int n;
    int linker[maxn];
    bool vis[maxn];
    
    bool dfs(int u) {
        for (int v = 0; v < n; v ++) {
            if (mp[u][v] && !vis[v]) {
                vis[v] = true;
                if (linker[v] == -1 || dfs(linker[v])) {
                    linker[v] = u;
                    return true;
                }
            }
        }
        return false;
    }
    
    int hungry() {
        memset(linker, -1, sizeof(linker));
        int res = 0;
        for (int i = 0; i < n; i ++) {
            memset(vis, 0, sizeof(vis));
            if (dfs(i)) res ++;
        }
        return res;
    }
    
    int main() {
        int u, v, cnt;
        while (~scanf("%d", &n)) {
            memset(mp, 0, sizeof(mp));
            for (int i = 0; i < n; i ++) {
                scanf("%d: (%d)", &u, &cnt);
                for (int j = 0; j < cnt; j ++) {
                    scanf("%d", &v);
                    mp[u][v] = 1;
                }
            }
            printf("%d
    ", n - hungry() / 2);
        }
    
        return 0;
    }
  • 相关阅读:
    bzoj1618 / P2918 [USACO08NOV]买干草Buying Hay(完全背包)
    bzoj1617 / P2904 [USACO08MAR]跨河River Crossing
    bzoj1615 / P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler
    bzoj1613 / P1353 [USACO08JAN]跑步Running
    bzoj1612 / P2419 [USACO08JAN]牛大赛Cow Contest(Floyd)
    bzoj1611 / P2895 [USACO08FEB]流星雨Meteor Shower
    bzoj1610 / P2665 [USACO08FEB]连线游戏Game of Lines
    bzoj1609 / P2896 [USACO08FEB]一起吃饭Eating Together(最长不降子序列)
    bzoj1606 / P2925 [USACO08DEC]干草出售Hay For Sale(01背包)
    [bzoj1041][HAOI2008]圆上的整点
  • 原文地址:https://www.cnblogs.com/LinKArftc/p/4902744.html
Copyright © 2011-2022 走看看