HDU【1068】Girls And Boys(二分图+模板+最大独立集)

Girls and Boys

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


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

题解:

题意:

给一堆相互认识的人的标号,人数的人数和哪些人,求最多有几人都相互不认识,就是最大独立集问题

思路:

最大独立集=顶点数-最大匹配数,直接匈牙利算法搞出互相人数的人的最大两两匹配数,然后用总人数减一下就好了



其实这道题因为是二分图练习,所以也就直接往匈牙利算法上想了,因为题目确实有点难理解,所以我就以为只需要把最大匹配数给求出就好了,但发现算法是对的,但是就是结果不对,于是没办法去网上看看博客,发现自己理解错题意了,原来和这个题目是让求最多几个人都互不相认识,于是,也就接触了最大独立集这个思想,正如上面所写,所以只要在原代码上稍加改动就AC了 QWQ,英语越来越渣了,不行,回家后得学会英语了,代码如下(本题还需要注意的一点是,这道题输入有点怪,所以导致这样输入的话在找最大匹配数的时候其实是多算了一遍,因为输入的时候把关系都给说了两遍,所以最后求出来的最大匹配数得ans/2才对):

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
bool fr[505][505];
bool vis[505];
int f[505];
int N;
int o,u;
bool find(int x)
{
	for(int i=0;i<N;i++)
	{
		if(fr[x][i]&&!vis[i])
		{
			vis[i]=true;
			if(f[i]==-1)
			{
				f[i]=x;
				return true;
			}
			else if(find(f[i]))
			{
				f[i]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	while(~scanf("%d",&N))
	{   
	    memset(fr,false,sizeof(fr));
		memset(f,-1,sizeof(f));
		for(int i=0;i<N;i++)
		{
		scanf("%d: (%d)",&o,&u);
		for(int j=0;j<u;j++)
		{
			int x;
			scanf("%d",&x);
			fr[o][x]=true;
		}
	    }
	    int ans=0;
	    for(int i=0;i<N;i++)
	    {
	    	memset(vis,false,sizeof(vis));
	    	if(find(i))
	    	ans++;
		}
		ans=ans/2;
		printf("%d\n",N-ans); 
	}
	return 0;
}
总的来说,这种题完完全全就是模板题,模板也算好理解,所以今天的题基本上都是套了一个模板做的




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值