首页 > 代码库 > hdu1068Girls and Boys(二分匹配,最大独立集)

hdu1068Girls and Boys(二分匹配,最大独立集)

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
给出他们之间认识关系,求最大两两不认识。
#include<stdio.h>
#include<vector>
#include<string.h>
#include<iostream>
using namespace std;
vector<int>map[1005];
int vist[1005],match[1005];
int find(int u)
{
    for(int j=0;j<map[u].size();j++){
        int v=map[u][j];
        if(!vist[v]){
            vist[v]=1;
            if(match[v]==-1||find(match[v]))
            {
                match[v]=u; return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int n,a,b,m,ans;
    while(scanf("%d",&n)>0)
    {
        for(int i=0;i<=n;i++)
        map[i].clear(),match[i]=-1;
        for(int i=0;i<n;i++)
        {
            scanf("%d: (%d)",&a,&m);
            while(m--)
            {
                scanf("%d",&b); map[a].push_back(b);
            }
        }
        ans=0;
        for(int i=0;i<n;i++)
        {
            memset(vist,0,sizeof(vist));
            if(find(i)) ans++;
        }
        printf("%d\n",n-ans/2);
    }
}