首页 > 代码库 > poj 2771 Guardian of Decency

poj 2771 Guardian of Decency

Guardian of Decency
http://poj.org/problem?id=2771
Time Limit: 3000MS Memory Limit: 65536K
   

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

2435 M classicism programming0 M baroque skiing43 M baroque chess30 F baroque soccer827 M romance programming194 F baroque programming67 M baroque ping-pong51 M classicism programming80 M classicism Paintball35 M baroque ping-pong39 F romance ping-pong110 M romance Paintball

Sample Output

37

Source

四个条件有一个满足就可以同时带出去,那么四个条件都不满足就不能同时带出去

四个条件都不满足的两个人之间连一条边

然后求最大点独立集

#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<cmath>#include<algorithm>#define N 501using namespace std;int T,n,ans,tot;bool v[N];int match[N];int front[N],nxt[N*N],to[N*N];int number[N];struct node{    string music,sport;    char sex;}e[N];void add(int u,int v){    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;}bool go(int u){    for(int i=front[u];i;i=nxt[i])    {        if(v[to[i]]) continue;        v[to[i]]=true;        if(!match[to[i]]||go(match[to[i]]))        {            match[to[i]]=u;            return 1;        }        }    return 0;}int main(){    scanf("%d",&T);    string b,c;    char a;    while(T--)    {        tot=ans=0;        memset(front,0,sizeof(front));        memset(match,0,sizeof(match));        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf("%d",&number[i]);            cin>>e[i].sex>>e[i].music>>e[i].sport;        }        for(int i=1;i<=n;i++)         for(int j=1;j<=n;j++)        {            if(i==j) continue;            if(abs(number[i]-number[j])>40) continue;            else if(e[i].sex==e[j].sex) continue;            else if(e[i].music!=e[j].music) continue;            else if(e[i].sport==e[j].sport) continue;            add(i,j);        }        for(int i=1;i<=n;i++)         {            memset(v,0,sizeof(v));            if(go(i)) ans++;        }        printf("%d\n",(n*2-ans)/2);    }}

 

poj 2771 Guardian of Decency