1. 程式人生 > >POJ 2771 Guardian of Decency 【最大獨立集】

POJ 2771 Guardian of Decency 【最大獨立集】

傳送門:http://poj.org/problem?id=2771

Guardian of Decency
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 6456   Accepted: 2668

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

Source

Northwestern Europe 2005

 

題意概括:

有 N 個學生,依次給出他們的身高、性別、喜歡音樂的型別、喜歡的運動。

老師給出了 四條 不可能成為親密關係的條件。老師想帶儘可能多的學生,但是有不想他們發展成為親密戀人(兩兩至少滿足一條上述的條件)。

問最多能帶多少學生?

解題思路:

我們把條件反過來建圖,即有可能成為親密關係的連邊。

那麼就轉換為求他們的最大獨立集了。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #define INF 0x3f3f3f3f
 6 using namespace std;
 7 const int MAXN = 505;
 8 struct date
 9 {
10     int h;
11     char mf[2], music[101], spo[101];
12 }pp[MAXN];
13 
14 struct Edge
15 {
16     int v, nxt;
17 }edge[MAXN*MAXN];
18 
19 int head[MAXN],cnt;
20 int linker[MAXN];
21 bool used[MAXN];
22 int N;
23 
24 void init()
25 {
26     memset(head, -1, sizeof(head));
27     memset(linker, -1, sizeof(linker));
28     memset(edge, 0, sizeof(edge));
29     cnt = 0;
30 }
31 
32 void add(int from, int to)
33 {
34     edge[cnt].v = to;
35     edge[cnt].nxt = head[from];
36     head[from] = cnt++;
37 }
38 
39 bool Find(int x)
40 {
41     int v;
42     for(int i = head[x]; i != -1; i = edge[i].nxt){
43         v = edge[i].v;
44         if(!used[v]){
45             used[v] = true;
46             if(linker[v] == -1 || Find(linker[v])){
47                 linker[v] = x;
48                 return true;
49             }
50         }
51     }
52     return false;
53 }
54 
55 bool chk(date a, date b)
56 {
57     if(abs(a.h-b.h) <= 40){
58         if(a.mf[0] != b.mf[0]){
59             if(strcmp(a.music, b.music) == 0){
60                 if(strcmp(a.spo, b.spo) != 0){
61                     return true;
62                 }
63             }
64         }
65     }
66     return false;
67 }
68 
69 int main()
70 {
71     int T_case;
72     scanf("%d", &T_case);
73     while(T_case--){
74         init();
75         scanf("%d", &N);
76         for(int i = 1; i <= N; i++){
77             scanf("%d %s %s %s", &pp[i].h, &pp[i].mf, &pp[i].music, &pp[i].spo);
78             for(int j = 1; j < i; j++){
79                 if(chk(pp[i], pp[j])){
80                     add(i, j);
81                     add(j, i);
82                 }
83             }
84         }
85         int ans = 0;
86         for(int i = 1; i <= N; i++){
87             memset(used, 0, sizeof(used));
88             if(Find(i)) ans++;
89         }
90         printf("%d\n", N-ans/2);
91     }
92     return 0;
93 }
View Code