POJ 2771 Guardian of Decency

Guardian of Decency

Time Limit: 3000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2771
64-bit integer IO format: %lld      Java class name: Main
 
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

Source

 
解题:最大独立集,最大独立集等于点数-最大匹配数
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cstring>
 5 #include <cmath>
 6 using namespace std;
 7 const int maxn = 1000;
 8 struct STU {
 9     int height;
10     string sex,music,sport;
11 };
12 vector<STU>stus;
13 vector<int>e[maxn];
14 bool can(STU &a,STU &b){
15     int cao = a.height - b.height;
16     if(cao < 0) cao = -cao;
17     return cao<=40 && a.sex!=b.sex&&a.music==b.music&&a.sport!=b.sport;
18 }
19 int Link[maxn];
20 bool T[maxn];
21 bool match(int u) {
22     for(int i = e[u].size()-1; i >= 0; --i) {
23         if(!T[e[u][i]]) {
24             T[e[u][i]] = true;
25             if(Link[e[u][i]] == -1 || match(Link[e[u][i]])) {
26                 Link[e[u][i]] = u;
27                 return true;
28             }
29         }
30     }
31     return false;
32 }
33 int main() {
34     int kase,n;
35     STU stu;
36     ios::sync_with_stdio(false);
37     cin>>kase;
38     while(kase--) {
39         stus.clear();
40         cin>>n;
41         for(int i = 0; i < maxn; ++i) e[i].clear();
42         for(int i = 0; i < n; ++i) {
43             cin>>stu.height>>stu.sex>>stu.music>>stu.sport;
44             stus.push_back(stu);
45         }
46         for(int i = 0; i < n; ++i)
47             for(int j = 0; j < n; ++j)
48                 if(can(stus[i],stus[j]))
49                     e[i].push_back(j);
50         int ret = 0;
51         memset(Link,-1,sizeof Link);
52         for(int i = 0; i < n; ++i) {
53             memset(T,false,sizeof T);
54             if(match(i)) ++ret;
55         }
56         cout<<(n-(ret>>1))<<endl;
57     }
58     return 0;
59 }
View Code

 

posted @ 2015-08-02 16:31  狂徒归来  阅读(324)  评论(0编辑  收藏  举报