*HDU 1068 二分图

本文介绍了一种解决二分图最大独立集问题的方法,通过匈牙利算法找到最大匹配,进而求得最大独立集的大小。适用于寻找学生间无浪漫关系的最大群体规模。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Girls and Boys

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


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 // 模板 匈牙利算法dfs 最大独立集=总结点数-最大匹配(有向图);最大独立集=总结点数-最大匹配/2(无向图),(图中任意两个顶点都不相连的顶点集合。)
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 int mp[1003][1003],vis[1003],link[1003];
 7 int Mu,Mv,n;  //左集合点数,右集合点数,总点数
 8 int dfs(int x)      //找增广路。
 9 {
10     for(int i=0;i<Mv;i++)
11     {
12         if(!vis[i]&&mp[x][i])
13         {
14             vis[i]=1;
15             if(link[i]==-1||dfs(link[i]))
16             {
17                 link[i]=x;
18                 return 1;
19             }
20         }
21     }
22     return 0;
23 }
24 int Maxcon()
25 {
26     int ans=0;
27     memset(link,-1,sizeof(link));  
28     for(int i=0;i<Mu;i++)
29     {
30         memset(vis,0,sizeof(vis));
31         if(dfs(i)) ans++;
32     }
33     return ans;
34 }
35 int main()
36 {
37     int a,b,c;
38     while(scanf("%d",&n)!=EOF)
39     {
40         memset(mp,0,sizeof(mp));
41         for(int i=0;i<n;i++)
42         {
43             scanf("%d: (%d)",&a,&c);
44             while(c--)
45             {
46                 scanf("%d",&b);
47                 mp[a][b]=mp[b][a]=1;
48             }
49         }
50         Mu=Mv=n;
51         printf("%d\n",n-Maxcon()/2);
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6066555.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值