HDU 1068 (Maximum Independent Set)

Source: Internet
Author: User
Girls and boys time limit: 20000/10000 ms (Java/other) memory limit: 65536/32768 K (Java/other) total submission (s): 2 accepted submission (s): 1 Font :{
Profont ('times new Roman ')
} "> Times New Roman | {
Profont ('verdana ')
} "> Verdana | {
Profont ('Georgia ')
} "> Georgia font size :{
Profontadd (-1)
} "> Detail {
Profontadd (1)
} "> → {
Objfolder ('procon ')
} "> Problem descriptionthe 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 shocould write to standard output a line containing the result .{
Objfolder ('prosamplein ')
} "> 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
{
Objfolder ('prosampleout ')
} "> Sample output
52
 
2425984 2010-05-07 00:34:24 Accepted 1068 2546 Ms 4196 K 836 B C ++ Chen
This topic examines the largest independent set. The largest independent set refers to the set of vertices without edges between two pairs. the most independent set of vertices becomes the largest independent set.
The maximum independent set of the Bipartite Graph = number of nodes-(minus sign) the maximum number of matching nodes
Because the number of nodes is not given in the question, I used a large array to save it, which should be time-consuming.
Note that mat [from] [to] = mat [to] [from] = 1. Only by dividing the result by two can we get the correct result. This is because, for example, 1 and 2 are related to 2 and
1 has the same relationship. Only when all values are assigned 1 and divided by 2 can the maximum matching result be obtained.
#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);
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.