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 }

原文地址:https://www.cnblogs.com/mjtcn/p/8559180.html

时间: 2024-11-12 04:06:45

POJ 2771 Guardian of Decency (二分图最大点独立集)的相关文章

poj 2771 Guardian of Decency【最大点独立集】

K - Guardian of Decency Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2771 Appoint description:  System Crawler  (2014-08-17) Description Frank N. Stein is a very conservative high-school teac

POJ - 2771 Guardian of Decency 二分图 最大匹配数

题目大意:有n个人要参加一项活动,活动要求参加的人里面尽量不要有couples,主办方提出了四个降低couples的方法: 1.两个人的身高差大于40 2.性别相同 3.喜欢的音乐风格不同 4.喜欢的运动相同 只要满足其中的一项就认定两人不是couples 现在给出n个人的四项数据,问最多能邀请到多少人 解题思路:这题和Poj 1466 Girls and Boys这题很相似,只不过这题给的条件不是直接给出的,而是要我们自己去找的 只要两个人四个条件都不满足,就可以认为他们是couples了(相

poj——2771 Guardian of Decency

poj——2771    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5916   Accepted: 2458 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he i

poj 2771 Guardian of Decency 解题报告

题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距  > 40cm 2.相同性别 3.喜欢的音乐种类  不同 4.有共同喜爱的 运动 只要满足其中这4个条件中的一个(当然越多越好啦),就可以将他们编为一组啦(一组两个人),求能被编为一组的最多组数. 这题实质上求的是二分图的最大独立集.  最大独立集 = 顶点数 - 最大匹配数 可以这样转化:两个人至少

poj 2771 Guardian of Decency

Guardian of Decency http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K       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 som

poj 2771 Guardian of Decency(最大独立数)

题意:人与人之间满足4个条件之一即不能成为一对(也就说这4个条件都不满足才能成为一对),求可能的最多的单身人数. 思路:把男女分为两部分,接下来就是二分图的匹配问题.把能成为一对的之间连边,然后求出最大匹配.题目要求的是最大独立数. 最大独立数=顶点数-最大匹配数 #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #

poj 3342 Party at Hali-Bula 判断二分图最大点独立集是否唯一

题意: 给一个二分图,判断最大点独立集是否唯一. 分析: 匈牙利算法. 代码: //poj 3342 //sep9 #include <iostream> #include <string> #include <map> using namespace std; const int maxN=212; int n; int M,v1,v2; bool g[maxN][maxN]; bool vis[maxN]; int link[maxN]; map<string

POJ 1466 Girls and Boys【最大点独立集】

大意:最大点独立集 分析:最大点独立集 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 505; 8 9 int n; 10 int Link[maxn]; 11 int vis[maxn]; 12 vector<int> G[maxn

UVA 12083 - Guardian of Decency(二分图最大匹配)

UVA 12083 - Guardian of Decency 题目链接 题意:给定一些男女,满足身高差不大于40,喜欢同一种音乐,不喜欢同一种体育项目,并且性别不同,就可能发生关系,现在老师要带一些男女出去玩,要求不能有一对发生关系,问最多能带多少人 思路:分男女,把会发生关系的连边,然后做最大匹配,最后n-最大匹配就是最多能带的人 代码: #include <cstdio> #include <cstring> #include <string> #include