uva 12083 Guardian of Decency (二分图匹配)
来源:互联网 发布:数据采集系统应用领域 编辑:程序博客网 时间:2024/04/20 19:59
uva 12083 Guardian of Decency
Description
Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that some of them might become couples. While you can never exclude this possibility, he has made some rules that he thinks indicates a low probability two persons will become a couple:
Their height differs by more than 40 cm.They are of the same sex.Their preferred music style is different.Their favourite sport is the same (they are likely to be fans of different teams and that would result in fighting).
So, for any two persons that he brings on the excursion, they must satisfy at least one of the requirements above. Help him find the maximum number of persons he can take, given their vital information.
Input
The first line of the input consists of an integer T ≤ 100 giving the number of test cases. The first line of each test case consists of an integer N ≤ 500 giving the number of pupils. Next there will be one line for each pupil consisting of four space-separated data items:
an integer h giving the height in cm;a character 'F' for female or 'M' for male;a string describing the preferred music style;a string with the name of the favourite sport.
No string in the input will contain more than 100 characters, nor will any string contain any whitespace.
Output
For each test case in the input there should be one line with an integer giving the maximum number of eligible pupils.
Sample Input
2
4
35 M classicism programming
0 M baroque skiing
43 M baroque chess
30 F baroque soccer
8
27 M romance programming
194 F baroque programming
67 M baroque ping-pong
51 M classicism programming
80 M classicism Paintball
35 M baroque ping-pong
39 F romance ping-pong
110 M romance Paintball
Sample Output
3
7
题目大意:Frank是一个思想有些保守的老师。有一次,他需要带一些学生出去旅游,但又怕其中有一些学生在旅途中萌生爱意。为了降低这种事情发生的概率,他决定确保带出去的任意两个学生至少要满足下面4条中的一条:
1)身高相差大于40厘米。
2)性别相同。
3)最喜欢的音乐属于不同的类型。
4)最喜欢的体育比赛相同。
你的任务是帮助Frank挑选尽量多的学生,使得任意两个学生至少满足上述条件中的一条。
解题思路:将可以匹配的男女建边,然后就是一个二分图匹配问题。最后用n减去求出的最大的匹配,就是答案。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace std;typedef long long ll;const int N = 505;const int M = 250005;struct Node { int height, id; char sex[5], music[105], sport[105]; }stu[N]; int n, e, ans;int head[N], pnt[M], Next[M], rec[N]; bool S[N], T[N]; void init() { e = 0; memset(head, -1, sizeof(head)); memset(rec, 0, sizeof(rec)); ans = 0;}bool check(int i,int j) { if(abs(stu[i].height - stu[j].height) > 40) return false; //身高差超过40cm if (strcmp(stu[i].music, stu[j].music)) return false; //喜欢的音乐类型相同 if (!strcmp(stu[i].sport, stu[j].sport)) return false; //喜欢的体育类型不同 return true; }void addEdge(int u,int v) { pnt[e] = v; Next[e] = head[u]; head[u] = e++; } void input() { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d %s %s %s", &stu[i].height, stu[i].sex, stu[i].music, stu[i].sport); } for(int i = 1; i <= n; i++) { if(stu[i].sex[0] == 'F') continue; //边只能从男生到女生 for(int j = 1; j <= n; j++) { //将可以匹配的男女建边 if(stu[j].sex[0] == 'F' && check(i,j)) addEdge(i,j); } }}bool match(int u) { S[u] = 1; for(int i = head[u]; i != -1 ; i = Next[i]) if(!T[pnt[i]]) { T[pnt[i]] = 1; if(!rec[pnt[i]] || match(rec[pnt[i]])) { rec[pnt[i]] = u; return true; } } return false; } void hungary() { for(int i = 1; i <= n; i++) { memset(S, 0, sizeof(S)); memset(T, 0, sizeof(T)); if(match(i)) ans++; }}int main() { int T; scanf("%d", &T); while(T--) { init(); input(); hungary(); printf("%d\n", n - ans); } return 0; }
- UVA 12083 - Guardian of Decency(二分图最大匹配)
- uva 12083 Guardian of Decency (二分图匹配)
- uva 12083 - Guardian of Decency(二分图匹配)
- UVA-12083-Guardian of Decency
- UVA 12083 Guardian of Decency
- POJ - 2771 Guardian of Decency 二分图 最大匹配数
- POJ 2771 Guardian of Decency(二分匹配-hungary)
- Guardian of Decency UVA
- Guardian of Decency UVA
- Guardian of Decency - UVa 12083 二分图最大独立点集
- Guardian of Decency hoj poj 二分图匹配 不错的题
- POJ 2771 Guardian of Decency(最大独立集、二分图匹配)
- POJ 2771 Guardian of Decency(二分图最大独立集)
- POJ 2771 Guardian of Decency(二分图最大独立集)
- POJ 2771 Guardian of Decency(二分图最大独立集)
- UVALive3415[Guardian of Decency] 二分图最大独立集模型
- LA 3415 Guardian of Decency (二分图最大独立集)
- UVA 12083 Guardian of Decency(最大独立集)
- 关于RadioGroup某个item设置为check后,会一直选中的问题
- c++友元实现操作符重载
- Best Time to Buy and Sell Stock II
- 1001.Rational Sum (20)
- A1075 . PAT Judge (25)
- uva 12083 Guardian of Decency (二分图匹配)
- USACO Ski Course Design解析和C语言实现
- Hibernate入门
- 【LeetCode】(58)Length of Last Word(Easy)
- swust oj 2297 逆序数 (dp)
- canvas 基本点理解
- 关于setTimeout函数中的闭包问题
- AsyncTask基本原理
- 回文词