POJ2771 Guardian of Decency(二分图)

POJ 2771 http://poj.org/problem?id=2771

Guardian of Decency
Time Limit: 3000MS		Memory Limit: 65536K
Total Submissions: 6768		Accepted: 2784
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
Source

Northwestern Europe 2005

 

 

题目大意:

这个题目题干比较长...大意是说一个比较保守的老师要带队出去,但是不希望他带出去的学生中创造恋爱的机会。

在老师认为,以下情况中两个学生不会恋爱:

1.身高差大于40(确实差太多了...)
2.同一性别
3.音乐口味不同
4.体育偏好相同

要求带出去的学生都不会互相喜欢且要求人数最大化。

 

解题思路:

先根据男女划分成二分图。将以上条件作为judge,满足的即不能成对,否则即可连线。

题目所求即为最大独立数(最大独立点集)

最大独立数 :选取最多的点,使任意所选两点均不相连 

且:

最大独立数 = 顶点数 - 最大匹配数

因此,用匈牙利算法求出最大匹配数之后套用公式即可。

 

AC代码:

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 550;
int T;
int map[N][N];
bool vis[N];
int match[N];
int n;
struct node{
    int h;
    string male,music,spo;
}s[N];
bool dfs(int u) {
    for(int v = 1; v <= N; v++) {
        if(map[u][v] && !vis[v]) {
            vis[v] = true;
            if(match[v] == -1 || dfs(match[v])) {
                match[v] = u;
                return true;
            }
        }
    }
    return false;
}
bool judge(node a, node b) {
    if(abs((double)(a.h - b.h)) > 40 || a.male == b.male || a.music != b.music || a.spo == b.spo)
        return false;
    return true;
}
int main() {
    cin >> T;
        while(T--) {
            memset(match, -1, sizeof match);
            memset(map, 0, sizeof map);
            cin >> n;
            for(int i = 1; i <= n; i++) {
                cin >> s[i].h >> s[i].male  >> s[i].music >> s[i].spo;
            }
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j < i;j++) {
                    if(judge(s[i],s[j])) {
                        map[i][j] = 1;
                        map[j][i] = 1;
                    }
                }
            }
            int sum = 0;
            for(int i = 1;i <= n; i++) {
                memset(vis, 0, sizeof vis);
                if(dfs(i)) {
                    sum++;
                }
            }
            cout << n - (sum / 2) << endl; //注意num / 2
        }

    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值