前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hduoj-----(1068)Girls and Boys(二分匹配)

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

作者头像
Gxjun
发布2018-03-22 14:15:58
4800
发布2018-03-22 14:15:58
举报
文章被收录于专栏:mlml

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

Southeastern Europe 2000

题意大致为: 有一个学校,男生女生要搭配,然后排除男神和男生搞基,女生和女生玩拉拉的意思,问最少有多少个落单的倒霉求?

其实就是变相的,最大匹配,就是求出了最大匹配,然后剩下的那些个倒霉求就是所求的答案嘛.....

代码:

代码语言:javascript
复制
 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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Girls and Boys
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档