天天看点

hdu 1068(最大独立集)Girls and Boys

Girls and Boys

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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      
2425984 2010-05-07 00:34:24 Accepted 1068 2546MS 4196K 836 B C++ chen
 本题考察的是最大独立集。最大独立集指的是两两之间没有边的顶点的集合,顶点最多的独立集成为最大独立集。      
二分图的最大独立集=节点数-(减号)最大匹配数      
由于题目中没有给出节点的个数,我就用了个较大的数组来保存,应该比较耗时。      
本题还要注意的一点是 mat[from][to]=mat[to][from]=1只有这样并将结果除以二,才能得到正确结果。这是因为比如说1与2有关系与2与      
1有关系相同,只有都赋值为1并除以二才能得到最大匹配的正确结果。      
#include<iostream>
using namespace std;      
int mat[1005][1005];
int useif[1005];
int link[1005];
int gl,gr;
int can(int t)
{
     int i;
     for(i=1;i<=gr;i++)
    if(useif[i]==0&&mat[t][i])
    {
       useif[i]=1;
       if(link[i]==-1||can(link[i]))
       {
            link[i]=t;
            return 1;
       }
  }
  return 0;
}      
int MaxMatch()
{
 int i,num=0;
 memset(link,-1,sizeof(link));
 for(i=1;i<=gl;i++)
 {
  memset(useif,0,sizeof(useif));
  if(can(i))
   num++;
 }
 return num;
}      
int main()
{
 int n,from,num,to,i,j,count;
 while(scanf("%d",&n)!=EOF)
 {
  gl=gr=n;
  memset(mat,0,sizeof(mat));
  count=n;
  while(count--)
  {
   scanf("%d: (%d)",&from,&num);
   from++;
   for(i=0;i<num;i++)
   {
    scanf("%d",&to);
    to++;
    mat[from][to]=mat[to][from]=1;
   }
  }
  printf("%d/n",n-MaxMatch()/2);
 }
}      

继续阅读