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 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

题目意思:

有n个人,每个人都有自己的身高、性别、喜欢的音乐、喜欢的运动,当满足以上4个条件中至少一个条件的时候则两个人可以同时被选出来,问最多能选出多少人。

思路:

最大独立集,男生放在左边,女生放在右边,建二分图。ans=n-最大匹配数。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 #include <set>
 9 using namespace std;
10
11 #define N 505
12
13 int max(int x,int y){return x>y?x:y;}
14 int min(int x,int y){return x<y?x:y;}
15 int abs(int x,int y){return x<0?-x:x;}
16
17 int n, m;
18 bool visited[N];
19 vector<int>ve[N];
20 int from[N];
21
22 struct node{
23     int h;
24     char sex[5];
25     char music[105];
26     char sport[105];
27 }a[N];
28
29 int march(int u){
30     int i, j, k, v;
31     for(i=0;i<ve[u].size();i++){
32         v=ve[u][i];
33         if(!visited[v]){
34             visited[v]=true;
35             if(from[v]==-1||march(from[v])){
36                 from[v]=u;
37                 return 1;
38             }
39         }
40     }
41     return 0;
42 }
43
44 main()
45 {
46     int t, i, j, k;
47     cin>>t;
48     while(t--){
49         scanf("%d",&n);
50         for(i=0;i<n;i++){
51             scanf("%d%s%s%s",&a[i].h,a[i].sex,a[i].music,a[i].sport);
52         }
53         for(i=0;i<=n;i++) ve[i].clear();
54         for(i=0;i<n;i++){
55             if(a[i].sex[0]==‘F‘){
56                 for(j=0;j<n;j++){
57                     if(a[j].sex[0]==‘M‘){
58                         if(abs(a[i].h-a[j].h)<=40&&strcmp(a[i].music,a[j].music)==0&&strcmp(a[i].sport,a[j].sport)){
59                             ve[i].push_back(j);
60                         }
61                     }
62                 }
63             }
64         }
65
66         int num=0;
67         memset(from,-1,sizeof(from));
68         for(i=0;i<n;i++){
69             memset(visited,false,sizeof(visited));
70             if(march(i)) num++;
71         }
72         printf("%d\n",n-num);
73     }
74 }
时间: 2024-08-29 16:56:18

POJ 2771 二分图(最大独立集)的相关文章

POJ 3692 Kindergarten(二分图最大独立集)

题意: 有G个女孩,B个男孩.女孩彼此互相认识,男孩也彼此互相认识.有M对男孩和女孩是认识的.分别是(g1,b1),.....(gm,bm). 现在老师要在这G+B个小孩中挑出一些人,条件是这些人都互相认识.问最多可以挑出多少人. 思路: 女孩之间互相认识,男孩之间互相认识,所以我们可以将连边定义为:不认识.即:若两个节点之间有连边,则两个节点互不认识. 故题意即为选出最多的点使得这些点任意两点之间没有连边.即选最少的点覆盖所有边.(二分图最大独立集/二分图最小点覆盖) 代码: vector<i

Girls and Boys POJ - 1466 【(二分图最大独立集)】

Problem DescriptionIn the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary

poj 2771 Guardian of Decency 解题报告

题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距  > 40cm 2.相同性别 3.喜欢的音乐种类  不同 4.有共同喜爱的 运动 只要满足其中这4个条件中的一个(当然越多越好啦),就可以将他们编为一组啦(一组两个人),求能被编为一组的最多组数. 这题实质上求的是二分图的最大独立集.  最大独立集 = 顶点数 - 最大匹配数 可以这样转化:两个人至少

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

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

poj——2771 Guardian of Decency

poj——2771    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5916   Accepted: 2458 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he i

poj 2771 Guardian of Decency【最大点独立集】

K - Guardian of Decency Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2771 Appoint description:  System Crawler  (2014-08-17) Description Frank N. Stein is a very conservative high-school teac

hdu 3829 二分图最大独立集

将孩子看做点,两个孩子间存在矛盾关系则连一条边,最后求二分图最大独立集即可 // // main.cpp // hdu3829 // // Created by Fangpin on 15/5/29. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <vector>

hihocoder 1158 质数相关 (二分图最大独立集 最大流ISAP求解)

#1158 : 质数相关 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被称为质数相关,是指S中存在两个质数相关的数,否则称S为质数无关.如{2, 8, 17}质数无关,但{2, 8, 16}, {3, 6}质数相关.现在给定一个集合S,问S的所有质数无关子集中,最大的子集的大小. 输入 第一行为一个数T,为数据组数.之后每组数据包含两行. 第一行为N,为集合S的大小.第二

杭电1068--Girls and Boys(二分图最大独立集)

Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8863    Accepted Submission(s): 4077 Problem Description the second year of the university somebody started a study on the romant