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
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
Source
題意大致為: 有一個學校,男生女生要搭配,然後排除男神和男生搞基,女生和女生玩拉拉的意思,問最少有多少個落單的倒黴求?
其實就是變相的,最大匹配,就是求出了最大匹配,然後剩下的那些個倒黴求就是所求的答案嘛.....
程式碼:
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("%dn",n-ans/2); 43 } 44 return 0; 45 }