HDU - 1068 Girls and Boys(二分匹配---最大独立集)

时间:2021-06-17 20:01:22

题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣。

分析:

1、若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即最大独立集问题。

2、最大独立集 = 顶点数 - 最大匹配数(匈牙利算法求解)。

3、将一个人拆成两个相同的人进行二分匹配,因此真正的最大匹配数应为得到的最大匹配数/2。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e3 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN][MAXN];
bool used[MAXN];
int vis[MAXN];
int n;
bool dfs(int x){
for(int i = 0; i < n; ++i){
if(a[x][i] && !used[i]){
used[i] = true;
if(vis[i] == -1 || dfs(vis[i])){
vis[i] = x;
return true;
}
}
}
return false;
}
int hungary(){
int ans = 0;
for(int i = 0; i < n; ++i){
memset(used, false, sizeof used);
if(dfs(i)) ++ans;
}
return ans;
}
int main(){
while(scanf("%d", &n) == 1){
memset(a, 0, sizeof a);
memset(vis, -1, sizeof vis);
for(int i = 0; i < n; ++i){
int x, m;
scanf("%d: (%d)", &x, &m);
for(int j = 0; j < m; ++j){
int y;
scanf("%d", &y);
a[x][y] = a[y][x] = 1;
}
}
printf("%d\n", n - hungary() / 2);
}
return 0;
}