POJ 2771 Guardian of Decency(二分图最大独立集)
来源:互联网 发布:linux查看crontab任务 编辑:程序博客网 时间:2024/05/04 09:34
题意:老师带学生出去旅游,但是担心学生会发生恋爱关系,所以带出去的学生至少要满足以下要求之中的一个:
1.身高相差40cm以上
2.同性
3.喜欢的音乐风格不同
4.喜欢的运动相同
问最多可以带出去多少学生?
思路:显然的一个二分图匹配,左边为男同学,右边为女同学,可以如果建图的时候正着建会很困难,所以我们反着建图,如果他们两不满足以上4个条件的任何一条,那么在这两个学生之间连一条边.这样我们就建立了一个二分图.(如果存在边肯定是左边与右边之间的边),那么该图的最大独立集中的任意两点就一定满足上面4个条件的至少一个
#include<cstdio>#include<cstring>#include<string>#include<vector>#include<iostream>#include<cmath>using namespace std;const int maxn=1000;struct Max_Match{ int n,m; vector<int> g[maxn]; bool vis[maxn]; int left[maxn]; void init(int n) { this->n=n; // this->m=m; for(int i=0;i<=n;i++) g[i].clear(); memset(left,-1,sizeof(left)); } bool match(int u) { for(int i=0;i<g[u].size();i++) { int v=g[u][i]; if(!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=0;i<n;i++) { memset(vis,0,sizeof(vis)); if(match(i)) ans++; } return ans; }}MM;int T;struct Node{double height;string music;string sport;string sex;}man[maxn],girl[maxn];int main(){int n,m;scanf("%d",&T);while (T--){int boy = 0;int gir = 0;scanf("%d",&n);for (int i = 1;i<=n;i++){ double temp;string sex;string mu,sp;cin >> temp >> sex >> mu >> sp; if (sex == "M"){man[boy].sex=sex; man[boy].height = temp; man[boy].music = mu;man[boy++].sport = sp;}else{girl[gir].sex=sex;girl[gir].height = temp;girl[gir].music = mu;girl[gir++].sport = sp;}} MM.init(boy);for (int i = 0;i<boy;i++)for (int j = 0;j<gir;j++){if (abs(man[i].height-girl[j].height)<=40 && (man[i].music == girl[j].music)&&(man[i].sport!=girl[j].sport))MM.g[i].push_back(j);}printf("%d\n",n-MM.solve());}}
0 0
- POJ 2771 Guardian of Decency(二分图最大独立集)
- POJ 2771 Guardian of Decency(二分图最大独立集)
- POJ 2771 Guardian of Decency(二分图最大独立集)
- 【二分图+最大独立集】北大 poj 2771 Guardian of Decency
- poj 2771 Guardian of Decency (二分图求最大独立集)
- POJ 2771 Guardian of Decency (二分图的最大独立集)
- POJ-2771-Guardian of Decency-求二分图最大独立集(匈牙利算法)
- POJ 2771 Guardian of Decency(最大独立集、二分图匹配)
- poj 2771 uva12083 Guardian of Decency(二分图最大独立集)
- UVALive3415[Guardian of Decency] 二分图最大独立集模型
- LA 3415 Guardian of Decency (二分图最大独立集)
- POJ 2771 Guardian of Decency (最大独立集Hungary)
- POJ 2771 Guardian of Decency(最大独立点集)
- POJ 2771 Guardian of Decency 最大独立集
- 【POJ】2771 Guardian of Decency 最大独立集
- poj 2771 Guardian of Decency 最大独立集
- poj 2771 Guardian of Decency(最大独立集)
- POJ - 2771 Guardian of Decency 二分图 最大匹配数
- leetcode(153 154) FindMinimuminRotatedSortedArray与FindMinimuminRotatedSortedArray II
- poj1390 2010.8.1
- 94. Binary Tree Inorder Traversal LeetCode
- 不容易系列之(3)—— LELE的RPG难题
- poj2231 2010.8.1
- POJ 2771 Guardian of Decency(二分图最大独立集)
- 1026. 程序运行时间
- NYOJ-71-独木舟上的旅行
- uva10617 2010.8.1
- 145. Binary Tree Postorder Traversal LeetCode
- poj1390 2010.8.2
- 字符串-《数据结构1000》-练习题
- 打印函数调用栈
- Bravo!Java学习笔记(1)---接口、抽象类、对象克隆、内部类