poj2771 二分图的最大独立集

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

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

/**
poj 2771 最大独立集
题目大意:一个老师要带一部分人,要求所带的人中不能有可能出现会搞对象的(不满足题目中的条件之一则有可能),
          问最多能带多少学生。
解题思路:所有可能搞对象的人匹配。求二分图的最大独立集就行。最大独立集=n-匹配数
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
const int maxn=505;

int n,high[maxn];
char mus[maxn][102],sex[maxn][2],spo[maxn][103];
int w[maxn][maxn];
int linker[maxn];
bool used[maxn];
bool dfs(int u)
{
    int v;
    for(v=0;v<n;v++)
    {
        if(w[u][v]&&!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<n;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u))res++;
    }
    return res;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%s%s%s",&high[i],sex[i],mus[i],spo[i]);
        }
        memset(w,0,sizeof(w));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(abs(high[i]-high[j])<=40&&strcmp(sex[i],sex[j])!=0&&strcmp(mus[i],mus[j])==0&&strcmp(spo[i],spo[j])!=0)
                {
                    w[i][j]=w[j][i]=1;
                }
            }
        }
        printf("%d\n",n-hungary()/2);
    }
    return 0;
}
时间: 2024-08-29 16:56:18

poj2771 二分图的最大独立集的相关文章

hdu3829 二分图的最大独立集

http://acm.hdu.edu.cn/showproblem.php?pid=3829 Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2851    Accepted Submission(s): 972 Problem Description The zoo have N cats and M dogs

「CODVES 1922 」骑士共存问题(二分图的最大独立集|网络流)&amp;dinic

首先是题目链接  http://codevs.cn/problem/1922/ 结果发现题目没图(心情复杂 然后去网上扒了一张图 大概就是这样了. 如果把每个点和它可以攻击的点连一条边,那问题就变成了求二分图的最大独立集了 (二分图最大独立集:即一个点集,集合中任两个结点不相邻),然后就是建图了. 题图非常好心的帮忙染色了,所以我们可以看出来,一个点可以到达的点和它的颜色是不一样的,所以只需要黑白染色就可以了,然后把黑点看作一个集合, 白点看作一个集合,又因为二分图最大独立集= 二分图最大匹配,

UVA 12168 - Cat vs. Dog(二分图匹配+最大独立集)

UVA 12168 - Cat vs. Dog 题目链接 题意:给定一些猫爱好者,和一些狗爱好者,每个人都有一个喜欢的猫(狗),和一个讨厌的狗(猫),要问现在给一种方案,使得尽量多的人被满足 思路:二分图匹配最大独立集,猫爱好者和狗爱好者矛盾的建边,做一次最大独立集即可 代码: #include <cstdio> #include <cstring> #include <vector> using namespace std; const int N = 505; in

uva12083 二分图 求最大独立集 转化为求最大匹配 由题意推出二分图

这题大白书例题 : Frank 是一个思想有些保守的高中老师,有一次,他需要带一些学生出去旅行,但又怕其中一些学生在旅途中萌生爱意.为了降低这种事情的发生概率,他决定确保带出去的任意两个学生至少要满足下面4条中的一条 1 身高相差大于40 2 性别相同 3 最喜欢的音乐属于不同的类型 4 最喜欢的体育比赛相同 任务帮组Frank挑选尽量多的学生,使得任意两个学生至少满足上述条件中的一条. 解  将不能同时去的人连一条边 就变成求最大独立集,即选择尽量多的节点,使得任意两个节点不相邻.|最大独立集

二分图的最大独立集 最大匹配解题 Hopcroft-Karp算法

二分图模型中的最大独立集问题:在二分图G=(X,Y;E)中求取最小的顶点集V* ⊂ {X,Y},使得边 V*任意两点之间没有边相连. 公式: 最大独立集顶点个数 = 总的顶点数(|X|+|Y|)- 最大匹配数 poj3692 题意:幼儿园有G个小女孩和B个小男孩,小女孩彼此之间互相认识,小男孩彼此之间互相认识.一些小女孩与一些小男孩之间也互相认识.现在老师要选一些小朋友做一个游戏,这些小朋友之间必须互相认识.问老师最多可以选多少个小朋友. 解题:满足X集合,Y集合,E边集合的题目可以用二分图模型

POJ - 1466 Girls and Boys 二分图+最大独立集

题目大意:有n个学生,某些学生之间存在着一种特殊的关系...现在要找出m个学生,要求这m个学生之间的任意两人不存在这种特殊的关系 解题思路:二分图问题,因为没办法划分成相应的两个集合且特殊关系是对称的,所以可以将两个点集都设置为n个点,求出最大匹配后再除以2即可得到(因为关系是对称的,所以所求得的最大匹配是双倍的) 得到最大匹配了,可以由定理得到 最大独立集 = n - 最大匹配数 #include<cstdio> #include<vector> #include<cstr

POJ 2771 二分图(最大独立集)

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

poj 1466 Girls and Boys(二分图的最大独立集)

http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11085   Accepted: 4956 Description In the second year of the university somebody started a study on the romantic relations between the students

POJ1466 Girls and Boys【二分图最大独立集】

题目链接: http://poj.org/problem?id=1466 题目大意: 有N个学生,他们之间的某些人比较暧昧,只有认识的人能组成一个集合.问:最多能组成 多少个集合,使得这几个集合之间的学生都没有任何关系. 思路: 从N个图中选出M个点,使得这M个点两两之间没有边,求最大的M是多少.二分图最大独立 集问题.本来应该以男生.女生各一边建二分图求最大独立集,但是这里只有N个点,没有告 诉男生.女生的编号.那么以N个学生为一边.再以N个学生为另一边.将相互联系的人之间 建边.然后求最大匹