POJ-2771 Guardian of Decency 最大独立子集

不能恋爱问题。

代码如下:

#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;

int N;

int G[505][505], visit[505], marry[505];

struct P
{
int h;
char sex[5], style[105], fav[105];
}p[505];

int judge(int x, int y)
{
if (abs(p[x].h-p[y].h)>40)
return 0;
else if (p[x].sex[0] == p[y].sex[0])
return 0;
else if (strcmp(p[x].style, p[y].style))
return 0;
else if (!strcmp(p[x].fav, p[y].fav))
return 0;
else return 1;
}

int path(int u)
{
for (int i = 1; i <= N; ++i) {
if (!G[u][i] || visit[i]) {
continue;
}
visit[i] = 1;
if (!marry[i] || path(marry[i])) {
marry[i] = u;
return 1;
}
}
return 0;
}

int main()
{
int T, ans;
scanf("%d", &T);
while (T--) {
ans = 0;
memset(G, 0, sizeof (G));
memset(marry, 0, sizeof (marry));
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
scanf("%d %s %s %s", &p[i].h, p[i].sex, p[i].style, p[i].fav);
}
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (judge(i, j)) {
G[i][j] = 1;
}
}
}
for (int i = 1; i <= N; ++i) {
memset(visit, 0, sizeof (visit));
if (path(i)) {
++ans;
}
}
printf("%d\n", N-(ans>>1));
}
return 0;
}



你可能感兴趣的:(poj)