前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2771 Guardian of Decency

POJ 2771 Guardian of Decency

作者头像
attack
发布2018-04-11 13:19:08
6290
发布2018-04-11 13:19:08
举报
文章被收录于专栏:数据结构与算法

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

代码语言:javascript
复制
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

代码语言:javascript
复制
3
7

Source

Northwestern Europe 2005

对于都不满足条件的连边

这道问题就转换成了从中选取一些点使其两两不相邻

最大点独立集的裸题

最大点独立集=N-二分图最大匹配

这毒瘤题居然卡Dinic!!!

这种出题人就应该被挂起来淦!!

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=1001;
int vis[MAXN];
int link[MAXN];
int map[MAXN][MAXN];
int N;
bool dfs(int x)
{
    for(int i=1;i<=N;i++)
    {
        if(map[x][i]&&!vis[i])
        {
            vis[i]=1;
            if(!link[i]||dfs(link[i]))
            {
                link[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
struct S
{
    int tall;
    string a,b,c;
}s[MAXN];

int main()
{
    #ifdef WIN32
    freopen("a.in","r",stdin);
    #else
    #endif
    int Test;
    scanf("%d",&Test);
    while(Test--)
    {
        memset(vis,0,sizeof(vis));
        memset(link,0,sizeof(link));
        memset(map,0,sizeof(map));
        scanf("%d",&N);
        for(int i=1;i<=N;i++)
            cin>>s[i].tall>>s[i].a>>s[i].b>>s[i].c;
        for(int i=1;i<=N;i++)
            for(int j=1;j<=N;j++)
                if(i!=j&&s[i].a!=s[j].a&&s[i].b==s[j].b&&s[i].c!=s[j].c&&fabs(s[i].tall-s[j].tall)<=40 )
                    map[i][j]=1; 
        int ans=0;
        for(int i=1;i<=N;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",(N*2-ans)/2 );
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-02-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档