HDOJ 1068 Girls And Boys (最大独立集数)

来源:互联网 发布:qt程序员常用单词 编辑:程序博客网 时间:2024/05/19 04:55

原题链接

一、题目描述

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
70: (3) 4 5 61: (2) 4 62: (0)3: (0)4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0
 

Sample Output
52

二、题目分析

二分匹配的变形。利用结论 二分图的最大独立集数 = 节点数 - 最大匹配数。


三、AC代码

#include <iostream>#include <cstring>using namespace std;const int M = 500;int MAP[M][M];int  line[M];bool used[M];int n, m, ans;bool findx(int x) {    for(int i = 0; i < n; i++) {        if(MAP[x][i] && !used[i]) {            used[i] = true;            if(line[i] == -1 || findx(line[i])) {                line[i] = x;                return true;            }        }    }    return false;}int main() {    while (cin >> n) {        ans = 0;        memset(MAP, 0, sizeof(MAP));        memset(line, -1, sizeof(line));        for(int i = 0; i < n; i++) {            /*getchar();getchar();getchar();getchar();            cin >> m;            getchar();*/            int hh;            scanf("%d: (%d)", &hh, &m);            for(int j = 0; j < m; j++) {                int tmp; cin >> tmp;                MAP[i][tmp] = 1;            }        }        for(int i = 0; i < n; i++) {            memset(used, 0, sizeof(used));            if(findx(i)) ans++;        }        //cout << ans << endl;        cout << n - ans/2 << endl;  //因为输入数据是双向的,比如有1-->6,就有6-->1,所以ans/2.    }    return 0;}


原创粉丝点击