POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)

Posted Reqaw’s Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)相关的知识,希望对你有一定的参考价值。

题目链接:

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
  1 /*
  2 问题
  3 输入n个人的信息,如果他们满足几个条件就视为存在成为一对的关系,求不能成为一对的最大子独立集的顶点个数 
  4 
  5 解题思路
  6 图论的题很绕,让我们理清关系,求最大子图,就是挑几个人,他们相互之间都存在一种不能成为一对的关系,最多有多少人
  7 做图的时候,将存在能成为一对的关系的置为1,否则都置为0,跑一边匈牙利算法得到最大匹配数
  8 那么顶点数n减去最大匹配数就是最大子独立集的个数了。 
  9 */
 10 #include<cstdio>
 11 #include<iostream>
 12 #include<string>
 13 #include<cstring>
 14 #include<cmath>
 15 
 16 using namespace std;
 17 const int maxn=1100;
 18 int n,e[maxn][maxn],fn,mn;
 19 int cx[maxn],cy[maxn];
 20 int book[maxn];
 21 struct PER{
 22     int age;
 23     string sex,music,sport;
 24 }fper[maxn],mper[maxn];
 25 
 26 
 27 int ok(struct PER a, struct PER b);
 28 int maxmatch();
 29 int dfs(int u);
 30 
 31 int main()
 32 {
 33     int T,i,j;
 34     int x;
 35     char s1[100],s2[100],s3[100];
 36     scanf("%d",&T);
 37     while(T--){
 38         scanf("%d",&n);
 39         fn=mn=0;
 40         for(i=0;i<n;i++){
 41             scanf("%d%s%s%s",&x,s1,s2,s3);
 42             if(strcmp(s1,"F") == 0){
 43                 fper[fn].age=x;
 44                 fper[fn].sex=s1;
 45                 fper[fn].music=s2;
 46                 fper[fn].sport=s3;
 47                 fn++;
 48             }
 49             else{
 50                 mper[mn].age=x;
 51                 mper[mn].sex=s1;
 52                 mper[mn].music=s2;
 53                 mper[mn].sport=s3;
 54                 mn++;
 55             }
 56         }
 57         
 58         memset(e,0,sizeof(int)*maxn*maxn);
 59         for(i=0;i<mn;i++){
 60             for(j=0;j<fn;j++){
 61                 if(ok(mper[i],fper[j])) 
 62                     e[i][j]=1;
 63             }
 64         }
 65         printf("%d\n",n-maxmatch());
 66     }
 67     return 0;
 68 }
 69 
 70 int ok(struct PER a, struct PER b){
 71     if(abs(a.age - b.age) > 40)
 72         return 0;
 73     if(a.sex == b.sex)
 74         return 0;
 75     if(a.music != b.music)
 76         return 0;
 77     if(a.sport == b.sport)
 78         return 0;
 79         
 80     return 1;
 81 }
 82 
 83 int dfs(int u)
 84 {
 85     for(int i=0;i<fn;i++){
 86         if(book[i] == 0 && e[u][i]){
 87             book[i]=1;
 88             
 89             if(cy[i] == -1 || dfs(cy[i])){
 90                 cx[u]=i;
 91                 cy[i]=u;
 92                 return 1;
 93             }
 94         }
 95     }    
 96     return 0;
 97 }    
 98 
 99 int maxmatch()
100 {
101     int i;
102     memset(cx,-1,sizeof(cx));
103     memset(cy,-1,sizeof(cy));
104     int ans=0;
105     for(i=0;i<mn;i++){
106         if(cx[i] == -1){
107             memset(book,0,sizeof(book));
108             if(dfs(i))    ans++;
109         }
110     }
111     return ans;
112 }

 

以上是关于POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2771 Guardian of Decency

POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)

POJ 2771 Guardian of Decency (二分图最大点独立集)

POJ——T2271 Guardian of Decency

poj2771

zoj 2771 - Get Out of the Glass