hdu2458 2010.3.6

本文探讨了一个幼儿园游戏选拔问题,即如何从已知相互认识关系的孩子中挑选最多数量的孩子参与游戏,确保所有参与者彼此认识。该问题通过二分图匹配算法解决,采用一种特殊的建图方式:将没有直接关系的男孩和女孩之间建立连接。

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

hdu2458 2010.3.6

 

二分图,建图的方式很特别,会想不到——没有关系的建一条边

 

Kindergarten

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 111    Accepted Submission(s): 58

 

 

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. Inaddition to that, some girls and boys know each other. Now the teachers want topick some kids to play a game, which need that all players know each other. Youare 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 whoknow each other, respectively.

Each of the following M lines contains twointegers 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 theboys are numbered from 1 to B.

 

The last test case is followed by a linecontaining three zeros.

 

 

Output

For each test case, print a line containingthe test case number( beginning with 1) followed by a integer which is themaximum 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

 

 

Recommend

teddy

#include <stdio.h>
#include <string.h>

#define MAX 210

char map[MAX][MAX],yt[MAX];
int g,b,y[MAX];

int judge(int);

int main(void)
{
	int m,i,p,q,t,sum;

	t=0;
	scanf("%d %d %d",&g,&b,&m);
	while (g!=0 || b!=0 || m!=0)
	{
		t++;
		memset(map,0,sizeof(map));

		while (m>0)
		{
			scanf("%d %d",&p,&q);
			map[p][q]=1;
			m--;
		}

		sum=0;
		memset(y,0,sizeof(y));
		for (i=1;i<=g;i++)
		{
			memset(yt,0,sizeof(yt));
			if (judge(i))  sum++;
		}

		printf("Case %d: %d\n",t,g+b-sum);

		scanf("%d %d %d",&g,&b,&m);
	}

	return 0;
}

int judge(int a)
{
	int i;

	for (i=1;i<=b;i++)
	{
		if (!map[a][i] && !yt[i])
		{
			yt[i]=1;
			if (y[i]==0 || judge(y[i]))
			{
				y[i]=a;
				return 1;
			}
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值