admin管理员组文章数量:1658730
Guardian of Decency
Problem 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
T \leq 100
T≤100 giving the number of test cases. The first line
of each test case consists of an integer
N
≤
500
N\leq 500
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 orM
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 100 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
思路
把不能的人之间连边,求最大独立集
最大独立集
=
∣
V
∣
−
=|V|-
=∣V∣− 最大匹配
/*** Amber ***/
#pragma GCC optimize(3,"Ofast","inline")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
#define ls (rt<<1)
#define rs (rt<<1|1)
typedef long long ll;
template <typename T>
inline void read(T &x) {
x = 0;
static int p;
p = 1;
static char c;
c = getchar();
while (!isdigit(c)) {
if (c == '-')p = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c - 48);
c = getchar();
}
x *= p;
}
template <typename T>
inline void print(T x) {
if (x<0) {
x = -x;
putchar('-');
}
static int cnt;
static int a[50];
cnt = 0;
do {
a[++cnt] = x % 10;
x /= 10;
} while (x);
for (int i = cnt; i >= 1; i--)putchar(a[i] + '0');
puts("");
}
const double Pi=acos(-1);
const double eps=1e-6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const int maxn = 5e3+10;
struct node{
int h;
string male,style,sport;
}a[maxn];
int n;
int head[maxn];
int t;
bool check(int i,int j) {
if (abs(a[i].h - a[j].h) > 40 || a[i].male == a[j].male || a[i].style != a[j].style ||
a[i].sport == a[j].sport)
return 0;
return 1;
}
struct edge{
int v,nxt;
}e[maxn*maxn];
int vis[maxn];
inline void add(int u,int v) {
t++;
e[t].v = v;
e[t].nxt = head[u];
head[u] = t;
}
int linker[maxn];
bool dfs(int u) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (!vis[v]) {
vis[v] = 1;
if (linker[v] == -1 || dfs(linker[v])) {
linker[v] = u;
return 1;
}
}
}
return 0;
}
inline void work() {
read(n);
t = 0;
memset(head, 0, sizeof(head));
memset(linker, -1, sizeof(linker));
for (int i = 1; i <= n; i++) {
cin >> a[i].h >> a[i].male >> a[i].style >> a[i].sport;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j && check(i, j)) {
add(i, j);
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
if (dfs(i)) ans++;
}
print(n - ans/2);
}
int main() {
//freopen("1.txt","r",stdin);
int T = 1;
read(T);
while (T--) {
work();
}
return 0;
}
版权声明:本文标题:Guardian of Decency 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729814074a1213690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论