您的位置:首页 > 其它

UVA 12083 Guardian of Decency(最大独立集)

2015-08-25 14:49 274 查看
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24903

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

Frank要带一些学生参加短途旅行,但是害怕一些同学成为couples,列举了四个不是couple的因素,现在要选出一些同学参加短途旅行。当四个条件满足一条时就一定不是couple,反之满足四个反条件时就是couple,由此求出最大匹配,最大独立点集就是所要求的结果。最小点覆盖(最大匹配数) + 最大独立点集 = 总顶点数

由于有性别的区别,所以直接以此作为二分图的分界条件。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=510;
int abs(int x) {  return x<0?-x:x;  }
struct node{
    int  h;
    char music[105],sport[105];
}mper[maxn],wper[maxn];
int man,woman;
bool bmap[maxn][maxn],bmask[maxn];
int nx,ny;
int cx[maxn],cy[maxn];
int findpath(int u){
    for(int j=1;j<=ny;j++){
        if(bmap[u][j] && !bmask[j]){
             bmask[j]=1;
             if(cy[j]==-1 || findpath(cy[j])){
                  cy[j]=u;
                  cx[u]=j;
                  return 1;
             }
        }
    }
    return 0;
}
int maxmatch(){
    int res=0;
    memset(cx,-1,sizeof(cx));
    memset(cy,-1,sizeof(cy));
    for(int i=1;i<=nx;i++){
        if(cx[i]==-1){
             for(int j=1;j<=ny;j++)   bmask[j]=0;
             res+=findpath(i);
        }
    }
    return res;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int t,n;
    cin>>t;
    while(t--){
        scanf("%d",&n);
        man=woman=0;
        memset(bmap,0,sizeof(bmap));
        int he;
        char se[5],mu[105],sp[105];
        for(int i=1;i<=n;i++){
            scanf("%d%s%s%s",&he,se,mu,sp);
            if(se[0]=='M'){
                 mper[++man].h=he;
                 strcpy(mper[man].music,mu);
                 strcpy(mper[man].sport,sp);
            }
            else {
                wper[++woman].h=he;
                strcpy(wper[woman].music,mu);
                strcpy(wper[woman].sport,sp);
            }
        }
        for(int i=1;i<=man;i++){
            for(int j=1;j<=woman;j++){
                 if(abs(mper[i].h-wper[j].h)<=40 && strcmp(mper[i].music,wper[j].music)==0 && strcmp(mper[i].sport,wper[j].sport)!=0)
                     bmap[i][j]=1;
            }
        }
        nx=man;
        ny=woman;
        int ans=maxmatch();
        cout<<n-ans<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: