首页 > 代码库 > hduoj-----(1068)Girls and Boys(二分匹配)

hduoj-----(1068)Girls and Boys(二分匹配)

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7577    Accepted Submission(s): 3472


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
52
Source
Southeastern Europe 2000
 
 
题意大致为: 有一个学校,男生女生要搭配,然后排除男神和男生搞基,女生和女生玩拉拉的意思,问最少有多少个落单的倒霉求?
 
其实就是变相的,最大匹配,就是求出了最大匹配,然后剩下的那些个倒霉求就是所求的答案嘛.....
 
 
代码:
 1 #include<cstring> 2 #include<cstdio> 3 #include<cstdlib> 4 using namespace std; 5 const int maxn=1005; 6 int n,a,b,c; 7 bool mat[maxn][maxn]; 8 bool vis[maxn]; 9 int girl[maxn];10 bool check(int x){11     for(int i=0;i<n;i++){12         if(mat[x][i]==1&&!vis[i]){13             vis[i]=1;14             if(girl[i]==-1||check(girl[i])){15               girl[i]=x;16                return 1;17             }18         }19     }20     return 0;21 }22 int main()23 {24     //freopen("test.in","r",stdin);25     while(scanf("%d",&n)!=EOF){26         memset(mat,0,sizeof(mat));27         memset(girl,-1,sizeof(girl));28         for(int i=0;i<n;i++){29           scanf("%d: (%d)",&a,&b);30           while(b--){31               scanf("%d",&c);32               mat[a][c]=1;33           }34         }35         int ans=0;36         for(int j=0;j<n;j++){37             memset(vis,0,sizeof(vis));38             if(check(j))ans++;39         }40     /*通过最大二分匹配,我们得到了最大匹配数,但是由于男生女生41     都算了一遍,所以是不是就得除以二。这样就是最大匹配数了*/42         printf("%d\n",n-ans/2);43     }44   return 0;45 }
View Code