POJ 2771 Guardian of Decency 最大独立集

POJ 2771 Guardian of Decency 最大独立集

题意:

把可能产生关系的连接起来,然后用求二分图的最大独立集

最大独立集=总顶点数 - 匹配数/2

 

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack> 
#include<string.h>
using namespace std;
const int maxn=1100;
struct node
{
	int hight;
	char sex;
	string music;
	string sport;
}people[maxn];
int n,g[maxn][maxn],match[maxn],vis[maxn];
bool dfs(int u)
{
	for(int i=1;i<=n;i++)
	{
		if(!vis[i] && g[u][i])
		{
			vis[i]=1;
			if(match[i]==-1 || dfs(match[i]))
			{
				match[i]=u;
				return true;
			}
		}
	}
	return false;
}
int Hunary()
{
	int all=0;
	memset(match,0xff,sizeof(match));
	for(int i=1;i<=n;i++)
	{
		memset(vis,0,sizeof(vis));
		if(dfs(i))
		  all++;
	}
	return all;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
	   scanf("%d",&n);
	   for(int i=1;i<=n;i++)
	   {
	     cin>>people[i].hight>>people[i].sex>>people[i].music>>people[i].sport;   	
	   }	
	   memset(g,0,sizeof(g));
	   for(int i=1;i<=n;i++)
	   {
	   	for(int j=1;j<=n;j++)
	   	{
	   	    if(people[i].hight-people[j].hight>40 || people[j].hight-people[i].hight>40)
            {
                 continue;	
			}	
			if(people[i].sex==people[j].sex)
			{
				 continue;
			}
			if(people[i].music!=people[j].music)
			{
				continue;
			}
			if(people[i].sport==people[j].sport)
			{
				continue;
			}
			g[i][j]=1;
	    } 
	   }
	   int ans=Hunary();
	   printf("%d\n",n-ans/2);
	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值