Girls and Boys HDU (二分匹配入门题)

来源:互联网 发布:易迈互联和阿里云 编辑:程序博客网 时间:2024/06/05 09:12

                                    Girls and Boys

                                           T ime Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                           T otal Submission(s): 5722    Accepted Submission(s): 2561


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
5
2
 
题意:
     
      一题二分匹配求解最大独立集的入门题。 每次都被英语水平坑到底啊!!!!!!
     题目看了半天。题目就是最大独立集的定义。就是给你男生女生数量叫你求出在这些的人中,能够使他们不认识到最大集合。
    显然我们是不知道输入的人到底是男还是女,于是我们就把某一个人拆成2个计算。
   然后根据公式::
                二分图最大独立集合 = 节点数 - 最大匹配数
  但最后我们要把结果除以二,因为我们拆成了两个人计算,即:在计算过程中是不分男女的进行查找。
Ok,下面给出用两个代码,一个是用矩阵写的,用时多,而且写得有点搓。第二个是用邻接表写得,查找快速。
 
 
邻接矩阵:
 
#include <stdio.h>#include <string.h>#define CL(x,v);memset(x,v,sizeof(x));const int maxn = 1002;int num[maxn];bool used[maxn],map[maxn][maxn];int n;bool Find(int u){    for(int j = 0;j < n;j++)      if(!used[j]&&map[u][j])      {          used[j] = true;          if(num[j]==-1||Find(num[j]))          {              num[j] = u;              return true;          }      }    return false;}int Hungry(){    int res = 0;    CL(num,-1);    for(int i = 0;i < n;i++)    {        CL(used,0);        if(Find(i)) res++;    }    return res;}int main(){    int m,i,j,a,b;    while(~scanf("%d",&n))    {        CL(map,0);        for(i = 0;i < n;i++)        {            scanf("%d: (%d)",&a,&m);  //This is a good idea.            for(j = 0;j < m;j++)            {               scanf("%d",&b);               map[a][b] = true;            }        }        int ans = Hungry();        printf("%d\n",n-ans/2);    }    return 0;}

 
邻接表:
  
 
#include <stdio.h>#include <string.h>#define CL(x,v);memset(x,v,sizeof(x));const int maxn = 1000;int n,top = 0;bool used[maxn];int link[maxn];int head[maxn*maxn],next[maxn*maxn],num[maxn*maxn];void Add(int u,int v){    next[top] = head[u];    num[top] = v;    head[u] = top++;}bool Find(int u){    for(int v = head[u];v != -1;v = next[v])      if(!used[num[v]])      {          used[num[v]] = true;          if(link[num[v]] == -1||Find(link[num[v]]))          {              link[num[v]] = u;              return true;          }      }    return false;}int Hungry(){    int res = 0;    CL(link,-1);    for(int i = 0;i < n;i++)    {        CL(used,0);        if(Find(i))          res++;    }    return res;}int main(){    int i,j,m,a,b;    while(~scanf("%d",&n))    {        top = 0;        CL(head,-1);        for(i = 0;i < n;i++)        {            scanf("%d: (%d)",&a,&m);            for(j = 0;j < m;j++)            {                scanf("%d",&b);                Add(a,b);            }        }        int ans = Hungry();        printf("%d\n",n - ans/2);    }    return 0;}

 
 
原创粉丝点击