hdu2458 Kindergarten [二分匹配模板]

xiaoxiao2021-02-28  27

Kindergarten

hdu2458 题目链接

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1382 Accepted Submission(s): 733

Problem Description

In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know each other. In addition to that, some girls and boys know each other. Now the teachers want to pick some kids to play a game, which need that all players know each other. You are to help to find maximum number of kids the teacher can pick.

Input

The input consists of multiple test cases. Each test case starts with a line containing three integers G, B (1 ≤ G, B ≤ 200) and M (0 ≤ M ≤ G × B), which is the number of girls, the number of boys and the number of pairs of girl and boy who know each other, respectively. Each of the following M lines contains two integers X and Y (1 ≤ X≤ G,1 ≤ Y ≤ B), which indicates that girl X and boy Y know each other. The girls are numbered from 1 to G and the boys are numbered from 1 to B.

The last test case is followed by a line containing three zeros.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the maximum number of kids the teacher can pick.

Sample Input

2 3 3 1 1 1 2 2 3 2 3 5 1 1 1 2 2 1 2 2 2 3 0 0 0

Sample Output

Case 1: 3 Case 2: 4

Source

2008 Asia Hefei Regional Contest Online by USTC

解题思路

这题是说老师要带小朋友做游戏,男孩之间相互全部认识,女孩之间全部相互认识。现在要选一些人来做游戏,要求选取的人必须全部互相认识的,问最多选取多少人。

这里必须要求所有人全部都相互认识,正向考虑的话就比较麻烦了。然后我的想法就是不认识的人的关系定为0.求一个最大的匹配数,就表示这些匹配的人之间相互不认识,求出不认识的人的最大匹配数,然后拿总人数减去这些人之后其他的就是全部互相两两认识了。

二分匹配模板

程序代码

#include <stdio.h> #include <string.h> int n,g,b; int match[420]; int book[420]; int map[420][420]; int dfs(int x){ int i; for(i=1;i<=b;i++){ if(!map[x][i]&&book[i]==0){ book[i]=1; if(match[i]==0||dfs(match[i])){ //如果这个人没有匹配或者能更换对象 match[i]=x; return 1; } } } return 0; } int main() { int i,j,k; int u,v,t=1; while(scanf("%d%d%d",&g,&b,&k)!=EOF){ if(g+b+k==0) break; n=g+b; memset(map,0,sizeof(map)); memset(match,0,sizeof(match)); for(i=0;i<k;i++){ scanf("%d%d",&u,&v); map[u][v]=1; //这里是单向的,因为男孩1和女孩1不同 // map[v][u]=1; } k=0; for(i=1;i<=g;i++){ memset(book,0,sizeof(book)); if(dfs(i)==1) k++; } printf("Case %d: %d\n",t++,n-k); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2630405.html

最新回复(0)