POJ 2771 Guardian of Decency (二分图最大点独立集)

Posted HWIM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2771 Guardian of Decency (二分图最大点独立集)相关的知识,希望对你有一定的参考价值。

Guardian of Decency

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 6133   Accepted: 2555

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

 

题意

一共n个人,从中选出最多的人去旅游。当两个人可能成为couples时两个人不能同时去,可能成为couples的条件是:1、身高差小于等于40;2、不同性别;3、喜欢相同的音乐;4、喜欢不同的运动。

分析

如果两个人可能成为couples,连边,求最大点独立集。

二分图的最大点独立集 = n-最小点覆盖集=n-最大匹配数。

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<string>
 5 #include<iostream>
 6 
 7 using namespace std;
 8 const int N = 1010;
 9 const int INF = 1e9;
10 
11 struct Student{
12     int h;
13     string x,y,s; 
14 }data[510];
15 struct Edge{
16     int to,nxt,c;
17     Edge() {}
18     Edge(int x,int y,int z) {to = x,c = y,nxt = z;}
19 }e[1000100];
20 int q[1000100],L,R,S,T,tot = 1;
21 int dis[N],cur[N],head[N];
22 
23 void add_edge(int u,int v,int c) {
24     e[++tot] = Edge(v,c,head[u]);head[u] = tot;
25     e[++tot] = Edge(u,0,head[v]);head[v] = tot;
26 }
27 bool bfs() {
28     for (int i=1; i<=T; ++i) cur[i] = head[i],dis[i] = -1;
29     L = 1,R = 0;
30     q[++R] = S;dis[S] = 1;
31     while (L <= R) {
32         int u = q[L++];
33         for (int i=head[u]; i; i=e[i].nxt) {
34             int v = e[i].to;
35             if (dis[v] == -1 && e[i].c > 0) {
36                 dis[v] = dis[u]+1;q[++R] = v;
37                 if (v==T) return true;
38             }
39         }
40     }
41     return false;
42 }
43 int dfs(int u,int flow) {
44     if (u==T) return flow;
45     int used = 0;
46     for (int &i=cur[u]; i; i=e[i].nxt) {
47         int v = e[i].to;
48         if (dis[v] == dis[u] + 1 && e[i].c > 0) {
49             int tmp = dfs(v,min(flow-used,e[i].c));
50             if (tmp > 0) {
51                 e[i].c -= tmp;e[i^1].c += tmp;
52                 used += tmp;
53                 if (used == flow) break;
54             }
55         }
56     }
57     if (used != flow) dis[u] = -1;
58     return used;
59 }
60 int dinic() {
61     int ret = 0;
62     while (bfs()) ret += dfs(S,INF);
63     return ret;
64 }
65 void Clear() {
66     tot = 1;
67     memset(head,0,sizeof(head));
68 }
69 int main() {
70     int Case,n;
71     scanf("%d",&Case);
72     while (Case--) {
73         Clear();
74         scanf("%d",&n);
75         S = n+n+1;T = n+n+2; //-
76         for (int i=1; i<=n; ++i) {
77             scanf("%d",&data[i].h);
78             cin >> data[i].x >> data[i].y >> data[i].s;
79         }
80         for (int i=1; i<=n; ++i) 
81             for (int j=1; j<=n; ++j) { // 可能成为couples的连边
82                 if (abs(data[i].h-data[j].h) > 40) continue;
83                 if (data[i].x == data[j].x) continue;
84                 if (data[i].y != data[j].y) continue;
85                 if (data[i].s == data[j].s) continue;
86                 add_edge(i,j+n,1);
87             }
88         for (int i=1; i<=n; ++i) add_edge(S,i,1),add_edge(i+n,T,1); 
89         int ans = dinic();
90         printf("%d\n",n-ans/2);
91     }
92     return 0;
93 }

 

以上是关于POJ 2771 Guardian of Decency (二分图最大点独立集)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2771 Guardian of Decency

POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)

POJ 2771 Guardian of Decency (二分图最大点独立集)

POJ——T2271 Guardian of Decency

poj2771

zoj 2771 - Get Out of the Glass