POJ 2771 Guardian of Decency(二分图最大独立集)
http://poj.org/problem?id=2771
题意:
老师带学生出去旅游,但是担心学生会发生恋爱关系,所以带出去的学生至少要满足以下要求之中的一个:
1.身高相差40cm以上
2.同性
3.喜欢的音乐风格不同
4.喜欢的运动相同
问最多可以带出去多少学生?
分析:
对于任意两个学生,如果他们两不满足以上4个条件的任何一条,那么在这两个学生之间连一条边.这样我们就建立了一个二分图.(左边点集表示男同学,右边点集表示女同学. 如果存在边肯定是左边与右边之间的边).
然后我们只需要求出该图的最大独立集即可.(想想为什么最大独立集中的任意两点一定满足上面4个条件中的最少1个)
最大独立集 = n-最大匹配数.
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 500+10;
struct Max_Match
{
int n,m;
bool g[maxn][maxn];
bool vis[maxn];
int left[maxn];
void init(int n,int m)
{
this->n=n;
this->m=m;
memset(g,0,sizeof(g));
memset(left,-1,sizeof(left));
}
bool match(int u)
{
for(int v=1;v<=m;v++)if(g[u][v] && !vis[v])
{
vis[v]=true;
if(left[v]==-1 || match(left[v]))
{
left[v]=u;
return true;
}
}
return false;
}
int solve()
{
int ans=0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(match(i)) ans++;
}
return ans;
}
}MM;
struct Node
{
int height;
string music;
string sport;
Node(){}
Node(int height,string& s1,string& s2):height(height),music(s1),sport(s2){}
bool has_edge(Node& rhs)
{
return (abs(height-rhs.height)<=40 && music == rhs.music && sport!=rhs.sport);
}
}boy[maxn],girl[maxn];
int boy_num,girl_num;
int main()
{
int T; scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
boy_num=girl_num=0;
for(int i=0;i<n;i++)
{
int height;
string gender,music,sport;
cin>>height>>gender>>music>>sport;
if(gender[0]=='M')
{
boy[++boy_num]=Node(height,music,sport);
}
else
{
girl[++girl_num]=Node(height,music,sport);
}
}
MM.init(boy_num,girl_num);
for(int i=1;i<=boy_num;i++)
for(int j=1;j<=girl_num;j++)
{
if(boy[i].has_edge(girl[j])) MM.g[i][j]=true;
}
printf("%d\n",n - MM.solve());
}
return 0;
}
分享到:
相关推荐
poj 2771 Guardian of Decency.md
【二分图顶点覆盖->最小割->最大流->Dinic算法求解】 解题报告+AC代码 http://hi.csdn.net/!s/WKVPR0 ----> 我的所有POJ解题报告 http://blog.csdn.net/lyy289065406/article/details/6642573
poj 2903 Joy of Mobile Routing.md
poj 3174 Alignment of the Planets.md
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
业余爱好。所以,算法不一定好,CODING也不一定佳,效率不一定高,只是能通过online judge而已。
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
北大POJ2151-Check the difficulty of problems 解题报告+AC代码
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) ... (5)二分图的最大匹配 (匈牙利算法) (poj3041,poj3020) (6)最大流的增广路算法(KM算法). (poj1459,poj3436) ......
北大POJ2109-Power of Cryptography 解题报告+AC代码
POJ2942-Knights of the Round Table 【Tarjan算法】 解题报告+AC代码 http://hi.csdn.net/!s/F3L8HO ================================== 我的POJ所有解题报告:...
poj2492 A Bug's Life并查集应用的扩展,希望可以给大家带来用处
北大POJ2739-Sum of Consecutive Prime Numbers 解题报告+AC代码
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
北大POJ3083-Children of the Candy Corn 解题报告+AC代码
北大POJ初级-图算法 解题报告+AC代码
poj分类poj分类poj分类poj分类
北大POJ1159-Palindrome 解题报告+AC代码
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
POJ 1988 并查集。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。