Poj(2771),最大独立集
题目链接:http://poj.org/problem?id=2771
Guardian of Decency
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5517 | Accepted: 2322 |
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:
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.
- 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:
No string in the input will contain more than 100 characters, nor will any string contain any whitespace.
- 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
看到题意我笑了。
题意:人和人之间如果满足4个条件的任意一条就不会成为夫妻,给定一些人,问从中最多能选多少人且保证任意两人不可能成为夫妻。
分析:由于条件之一是性别相同则不能成为夫妻。我们根据性别把人划分为两个集合,每个人是一个结点构成二分图,若两个人可能成为夫妻则连一条边。然后最大独立集 = 顶点数 - 最大匹配数。
#include <stdio.h> #include <string.h> #include <math.h> bool maps[550][550]; bool use[550]; int match[550]; struct Stu { int h; char sex[2]; char music[50]; char sport[50]; }mans[550],womans[550]; int nman,nwoman; bool judge(Stu a,Stu b) { if(strcmp(a.music,b.music)) return false; if(!strcmp(a.sport,b.sport)) return false; if(fabs((a.h-b.h)*1.0)>40) return false; return true; } bool DFS(int u) { for(int i=0;i<nwoman;i++) { if(!use[i]&&maps[u][i]) { use[i] = true; if(match[i]==-1||DFS(match[i])) { match[i] = u; return true; } } } return false; } int main() { int t; scanf("%d",&t); while(t--) { memset(maps,false,sizeof(maps)); memset(match,-1,sizeof(match)); int num; nman = 0; nwoman = 0; scanf("%d",&num); for(int i=0;i<num;i++) { int height; char sex[2]; char musics[50]; char sports[50]; scanf("%d%s%s%s",&height,sex,musics,sports); if(sex[0]=='M') { mans[nman].h = height; strcpy(mans[nman].sex,sex); strcpy(mans[nman].music,musics); strcpy(mans[nman++].sport,sports); } if(sex[0]=='F') { womans[nwoman].h = height; strcpy(womans[nwoman].sex,sex); strcpy(womans[nwoman].music,musics); strcpy(womans[nwoman++].sport,sports); } } for(int i=0;i<nman;i++) { for(int j=0;j<nwoman;j++) { if(judge(mans[i],womans[j])) maps[i][j] = true; } } int ans = 0; for(int i=0;i<nman;i++) { memset(use,false,sizeof(use)); if(DFS(i)) ans++; } printf("%d\n",num-ans); } return 0; }