POJ 2771 Guardian of Decency 最大独立集
题意:
把可能产生关系的连接起来,然后用求二分图的最大独立集
最大独立集=总顶点数 - 匹配数/2
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<string.h>
using namespace std;
const int maxn=1100;
struct node
{
int hight;
char sex;
string music;
string sport;
}people[maxn];
int n,g[maxn][maxn],match[maxn],vis[maxn];
bool dfs(int u)
{
for(int i=1;i<=n;i++)
{
if(!vis[i] && g[u][i])
{
vis[i]=1;
if(match[i]==-1 || dfs(match[i]))
{
match[i]=u;
return true;
}
}
}
return false;
}
int Hunary()
{
int all=0;
memset(match,0xff,sizeof(match));
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
all++;
}
return all;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
cin>>people[i].hight>>people[i].sex>>people[i].music>>people[i].sport;
}
memset(g,0,sizeof(g));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(people[i].hight-people[j].hight>40 || people[j].hight-people[i].hight>40)
{
continue;
}
if(people[i].sex==people[j].sex)
{
continue;
}
if(people[i].music!=people[j].music)
{
continue;
}
if(people[i].sport==people[j].sport)
{
continue;
}
g[i][j]=1;
}
}
int ans=Hunary();
printf("%d\n",n-ans/2);
}
return 0;
}