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 a excursion, but he was afraid that some of them might become couples. While you can never exclude this possibility, he had made some rules that he thinks indicates a low probability S would become a couple:
- Their height differs by more than-CM.
- They is of the same sex.
- Their preferred music style is different.
- Their favourite sport is the same (they was likely to being fans of different teams and that would result in fighting).
So, for any of the persons this 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 the test cases. The first line of all test case consists of an integer n≤500 giving the number of pupils. Next There'll be a 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 would contain more than characters, nor would any string contain any whitespace.
Output
For each test case in the input there should is 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 P ing-pong110 M Romance Paintball
Sample Output
37
/**POJ 2771 maximum independent set the main idea: a teacher to bring a part of the people, asked to bring the person can not be able to engage in the object (not satisfied with one of the conditions in the subject is possible), ask the maximum number of students. The idea of solving a problem: all the people who might be doing it. The maximum independent set of the two graphs can be obtained. Maximum number of independent sets =n-matches */#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream># Include <math.h>using namespace Std;const int Maxn=505;int N,high[maxn];char mus[maxn][102],sex[maxn][2],spo[ Maxn][103];int w[maxn][maxn];int linker[maxn];bool used[maxn];bool dfs (int u) {int V; for (v=0;v<n;v++) {if (W[u][v]&&!used[v]) {used[v]=true; if (linker[v]==-1| | DFS (Linker[v])) {linker[v]=u; return true; }}} return false;} int Hungary () {int res=0; int u; memset (LINKER,-1,SIZEOF (linker)); for (u=0;u<n;u++) {memset (used,0,sizeof (used)); if (DFS (U)) res++; } return res; int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i=0;i<n;i++) {scanf ("%d%s%s%s", &high[i],sex[i],mus[i],spo[i]); } memset (W,0,sizeof (w)); for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (ABS (High[i]-high[j]) < ; =40&&strcmp (Sex[i],sex[j])!=0&&strcmp (Mus[i],mus[j]) ==0&&strcmp (Spo[i],spo[j])!=0) {w[i][j]=w[j][i]=1; }}} printf ("%d\n", N-hungary ()/2); } return 0;}
Maximum independent set of poj2771 binary graphs