您的位置:首页 > 其它

UVALive 3415 Guardian of Decency(最大独立集)

2016-05-14 03:31 387 查看
题目链接:

UVALive 3415 Guardian of Decency

题意:

刘汝佳的大白书P356页上的题目.

最大独立集.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 510;

int T, n, m, K, total;
int head[MAX_N * MAX_N], vis[MAX_N], match[MAX_N];

struct Student{
int height;
string sex, music, game;
}boy[MAX_N], girl[MAX_N];

struct Edge{
int to,next;
}edge[MAX_N * MAX_N];

inline bool check(int i, int j)
{
if(abs(boy[i].height - girl[j].height) > 40) return true;
if(boy[i].music != girl[j].music) return true;
if(boy[i].game == girl[j].game) return true;
return false;
}

inline void AddEdge(int from, int to)
{
edge[total].to = to;
edge[total].next = head[from];
head[from] = total++;
}

inline bool dfs(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next){
int to = edge[i].to;
if(vis[to]) continue;
vis[to] = 1;
if(match[to] == -1 || dfs(match[to])){
match[to] = u;
return true;
}
}
return false;
}

inline int Hungary()
{
int res = 0;
memset(match, -1, sizeof(match));
for(int i = 0; i < n; i++){
memset(vis, 0, sizeof(vis));
if(dfs(i)) res++;
}
return res;
}

int main()
{
IOS;
cin >> T;
while(T--){
cin >> K;
n = m = 0;
for(int i = 0; i < K; i++){
int h;
string tmp1, tmp2, tmp3;
cin >> h >> tmp1 >> tmp2 >> tmp3;
if(tmp1[0] == 'M') {
boy
.height = h, boy
.music = tmp2;
boy
.game = tmp3, n++;
}else {
girl[m].height = h, girl[m].music = tmp2;
girl[m].game = tmp3, m++;
}
}
total = 0;
memset(head, -1, sizeof(head));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(check(i, j) == false ) AddEdge(i, j);
}
}
int ans = Hungary();
cout << K - ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: