二分图最大独立集

http://poj.org/problem?id=1466

Girls and Boys
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 10206   Accepted: 4525

Description

In 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.

Input

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 (n <=500 ), for n subjects.

Output

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
题意:

列出每两对人之间是否有罗曼史,然后求一个最大的集合,使得这个集合中的每两个人之间都没有罗曼史

最大独立集=总点数-最大匹配(最小点覆盖)

分析:本题中男女性别未知,把所有人建立左边的点,所有人建立右边的点,然后连线,此时得到的最大匹配是实际上的2倍,有重复的

所以:最大独立集=N-最大匹配数/2;

程序:

#include"string.h"
#include"stdio.h"
#include"iostream"
#include"queue"
#include"string"
#include"map"
#define M 555
#define inf 999999999
using namespace std;
int G[555][555],y[555],use[555],x[555];
int finde(int u,int n)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(!use[i]&&G[u][i])
        {
            use[i]=1;
            if(y[i]==0||finde(y[i],n))
            {
                y[i]=u;
                x[u]=i;
                return 1;
            }
        }
    }
    return 0;
}
int max_match(int n)
{
    memset(y,0,sizeof(y));
    memset(x,0,sizeof(x));
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(!x[i])
        {
            memset(use,0,sizeof(use));
            ans+=finde(i,n);
        }
    }
    return ans;
}
int main()
{
    int n,i,a,b,j;
    char ch[222];
    while(scanf("%d",&n)!=-1)
    {
        memset(G,0,sizeof(G));
        for(i=1;i<=n;i++)
        {
            scanf("%d:",&a);
            scanf("%s",ch);
            int m=strlen(ch);
            int k=0;
            for(j=1;j<m-1;j++)
                k=10*k+ch[j]-'0';
            while(k--)
            {
                scanf("%d",&b);
                G[a+1][b+1]=1;
            }
        }
        int ans=max_match(n);
        printf("%d
",n-ans/2);
    }
}


原文地址:https://www.cnblogs.com/mypsq/p/4348220.html