admin管理员组

文章数量:1658610

Language: Guardian of Decency
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 4566 Accepted: 1922

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

二分图最大独立集与最小点覆盖是互补的,所以用总人数减去最大匹配即可。

居然是<=40

下面是代码:
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
struct A
{
    int high;
    string M;
    string music;
    string sport;
}stu[505];
int t[505][505];
int pre[505],n;
bool vis[505];
bool dfs(int u)
{
    for(int j=1;j<=n;j++)
    {
        if((!vis[j])&&t[u][j])
        {
            vis[j]=true;
            if(pre[j]==0||dfs(pre[j]))
            {
                pre[j]=u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int loop;
    cin>>loop;
    while(loop--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>stu[i].high>>stu[i].M>>stu[i].music>>stu[i].sport;
        }
        memset(t,0,sizeof(t));
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
            if((abs(stu[i].high-stu[j].high)<=40)&&(stu[i].M!=stu[j].M)&&(stu[i].music==stu[j].music)&&(stu[i].sport!=stu[j].sport))
                t[i][j]=t[j][i]=1;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,false,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        cout<<n-ans/2<<endl;
    }
    return 0;
}


本文标签: 独立