POJ 2771 Guardian of Decency (二分图最大点独立集)
2024-09-02 21:21:30
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
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream> using namespace std;
const int N = ;
const int INF = 1e9; struct Student{
int h;
string x,y,s;
}data[];
struct Edge{
int to,nxt,c;
Edge() {}
Edge(int x,int y,int z) {to = x,c = y,nxt = z;}
}e[];
int q[],L,R,S,T,tot = ;
int dis[N],cur[N],head[N]; void add_edge(int u,int v,int c) {
e[++tot] = Edge(v,c,head[u]);head[u] = tot;
e[++tot] = Edge(u,,head[v]);head[v] = tot;
}
bool bfs() {
for (int i=; i<=T; ++i) cur[i] = head[i],dis[i] = -;
L = ,R = ;
q[++R] = S;dis[S] = ;
while (L <= R) {
int u = q[L++];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (dis[v] == - && e[i].c > ) {
dis[v] = dis[u]+;q[++R] = v;
if (v==T) return true;
}
}
}
return false;
}
int dfs(int u,int flow) {
if (u==T) return flow;
int used = ;
for (int &i=cur[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (dis[v] == dis[u] + && e[i].c > ) {
int tmp = dfs(v,min(flow-used,e[i].c));
if (tmp > ) {
e[i].c -= tmp;e[i^].c += tmp;
used += tmp;
if (used == flow) break;
}
}
}
if (used != flow) dis[u] = -;
return used;
}
int dinic() {
int ret = ;
while (bfs()) ret += dfs(S,INF);
return ret;
}
void Clear() {
tot = ;
memset(head,,sizeof(head));
}
int main() {
int Case,n;
scanf("%d",&Case);
while (Case--) {
Clear();
scanf("%d",&n);
S = n+n+;T = n+n+; //-
for (int i=; i<=n; ++i) {
scanf("%d",&data[i].h);
cin >> data[i].x >> data[i].y >> data[i].s;
}
for (int i=; i<=n; ++i)
for (int j=; j<=n; ++j) { // 可能成为couples的连边
if (abs(data[i].h-data[j].h) > ) continue;
if (data[i].x == data[j].x) continue;
if (data[i].y != data[j].y) continue;
if (data[i].s == data[j].s) continue;
add_edge(i,j+n,);
}
for (int i=; i<=n; ++i) add_edge(S,i,),add_edge(i+n,T,);
int ans = dinic();
printf("%d\n",n-ans/);
}
return ;
}
最新文章
- AndroidPn源码分析(一)
- Swift3.0语言教程字符串与文件的数据转换
- thumbnailator图片处理
- Task+http请求
- 带删除小图标的EditText
- IOS-day01_OC中类的创建以及使用
- Python关于eval与json在字典转换方面的性能比较
- makefile简单helloworld
- [LeetCode]题解(python):009-Palindrome Number
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
- 在Visual Studio 2013 中使用C++单元测试
- LAMP与LNMP架构的区别及其具体的选择说明
- 大数据mapreduce俩表join之python实现
- git 常用命令收集
- 两数据库Dblink数据抽取blob
- Linux下软件包安装
- [PE484]Arithmetic Derivative
- [SQL] sql server中如何查看执行效率不高的语句
- MDK软件仿真常见问题
- Dozer 对象的";搬运工";