ACM 第五天

匈牙利算法(二分图匹配)

C - Courses

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:

. every student in the committee represents a different course (a student can represent a course if he/she visits that course)

. each course has a representative in the committee

Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is presented in the following format:

P N
Count1 Student1 1 Student1 2 ... Student1 Count1
Count2 Student2 1 Student2 2 ... Student2 Count2
......
CountP StudentP 1 StudentP 2 ... StudentP CountP

The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses . from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you'll find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.

There are no blank lines between consecutive sets of data. Input data are correct.

The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.

An example of program input and output:

Input
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
Output
YES
NO 
Sample Input
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
Sample Output
YES
NO 
复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define INF 0x3f3f3f3f
 4 
 5 int G[310][301],vis[310],used[310];
 6 int n,p;
 7 bool find(int u)//核心部分:寻找匈牙利算法的增广路
 8 {
 9     int i;
10     for(i=1;i<=n;i++)
11     {
12         if(!vis[i] && G[u][i])
13         {
14             vis[i]=1;
15             if(!used[i] || find(used[i]))
16             {
17                 used[i]=u;
18                 return true;
19             }
20         }
21     }
22     return false;
23 }
24 int main()
25 {
26     int a,b,i,ans,T;
27     scanf("%d",&T);
28     while(T--)
29     {
30         scanf("%d%d",&p,&n);
31         memset(G,0,sizeof(G));
32         ans=0;
33         for(i=1;i<=p;i++)
34         {
35             scanf("%d",&a);
36             while(a--)
37             {
38                 scanf("%d",&b);
39                 G[i][b]=1;
40             }
41         }
42         memset(used,0,sizeof(used));
43         for(i=1;i<=p;i++)
44         {
45             memset(vis,0,sizeof(vis));
46             if(find(i)) ans++;
47         }
48         if(ans==p) printf("YES\n");
49         else printf("NO\n");
50     }
51     return 0;
52 }
复制代码

 

B - The Accomodation of Students

There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.

InputFor each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.

Proceed to the end of file.

OutputIf these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
Sample Output
No
3

复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include <queue>
 4 using namespace std;
 5 bool vis[210];
 6 int G[210][201],used[210];
 7     int n,m;
 8 int find(int u)//核心部分:寻找匈牙利算法的增广路
 9 {
10     int i,n;
11     for(i=1; i<=n; i++)
12     {
13         if(!vis[i] && G[u][i])
14         {
15             vis[i]=true;
16             if(!used[i] || find(used[i]))
17             {
18                 used[i]=u;
19                 return 1;
20             }
21         }
22     }
23     return 0;
24 }
25 
26 int judge[210];
27 bool bfs()//广度优先搜索进行
28 {
29     memset(judge,-1,sizeof(judge));
30     queue<int> q;
31     q.push(1);
32     judge[1]=0;
33     while(!q.empty())
34     {
35         int v=q.front();
36         q.pop();
37         for(int i=1; i<=n; i++)
38         {
39             if(G[v][i])
40             {
41                 if(judge[i]==-1)
42                 {
43                     judge[i]=(judge[v]+1)%2;
44                     q.push(i);
45                 }
46                 else
47                 {
48                     if(judge[i]==judge[v])
49                         return false;
50                 }
51 
52             }
53         }
54     }
55     return true;
56 }
57 
58 int main()
59 {
60 
61     while(~scanf("%d%d",&n,&m))
62     {
63         memset(G,0,sizeof(G));
64         memset(used,0,sizeof(used));
65         int a,b;
66         for(int i=0; i<m; i++)
67         {
68             scanf("%d%d",&a,&b);
69             G[a][b]=1;
70             G[b][a]=1;
71         }
72         if(!bfs())
73         {
74             puts("No");
75             continue;
76         }
77         int ans=0;
78         for(int i=1; i<=n; i++)
79         {
80             memset(vis,0,sizeof(vis));
81             if(find(i));
82             ans++;
83         }
84         printf("%d\n",ans/2);
85     }
86     return 0;
87 }
复制代码

 

作者:白雪儿
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。

posted @   白雪儿  Views(198)  Comments(0Edit  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签
点击右上角即可分享
微信分享提示