'commit'
This commit is contained in:
57
TangDou/AcWing/ErFenTu/POJ3692.cpp
Normal file
57
TangDou/AcWing/ErFenTu/POJ3692.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
/*
|
||||
一群男孩女孩,同性之间都相互认识,但是异性之间只有某些人认识彼此。给出相互认识的异性的各自编号。
|
||||
求组成一个小队,这个小队里的人都相互认识。问这个小队最多能有多少人。
|
||||
|
||||
把相互不了解的人作为边构建二分图,这样题意是选择相互了解的人,那么是选择二分图的最大独立集,
|
||||
即总端点数-最大匹配(最小点覆盖)
|
||||
*/
|
||||
const int N = 210;
|
||||
int g[N][N];
|
||||
int n, m, e;
|
||||
|
||||
int match[N], st[N]; // match表示女生喜欢的男生,st表示女生是否被匹配到
|
||||
int dfs(int u) {
|
||||
for (int i = 1; i <= m; i++) {
|
||||
if (!g[u][i] && !st[i]) {
|
||||
st[i] = 1;
|
||||
if (match[i] == -1 || dfs(match[i])) {
|
||||
match[i] = u;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int cas = 0;
|
||||
while (~scanf("%d%d%d", &n, &m, &e)) {
|
||||
cas++;
|
||||
if (n == 0 && m == 0 && e == 0) break;
|
||||
// 初始化匈牙利算法匹配数据
|
||||
memset(match, -1, sizeof match);
|
||||
|
||||
// 邻接矩阵
|
||||
memset(g, 0, sizeof g); // g[i][j]=1:表示i与j互相了解
|
||||
while (e--) {
|
||||
int a, b;
|
||||
scanf("%d%d", &a, &b);
|
||||
g[a][b] = 1;
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
// 枚举集合A中的点,n为集合A中点的个数,即男生的个数
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// 这里每次都需要全部清0
|
||||
memset(st, 0, sizeof st);
|
||||
if (dfs(i)) ans++;
|
||||
}
|
||||
// 最大独立集
|
||||
printf("Case %d: %d\n", cas, n + m - ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user