题意
传送门 POJ 3692
题解
求两两之间互有关系的点集,用补集的思想可以转换为求补图的最大独立集,最终转化为最大二分匹配问题。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 0x3f3f3f3f
#define delta 0.85
#define eps 1e-10
#define PI 3.14159265358979323846
using namespace std;
#define MAX_V 400
int V;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V];
void add_edge(int u, int v){
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v){
used[v] = true;
for(int i = 0; i < G[v].size(); i++){
int u = G[v][i], w = match[u];
if(w < 0 || (!used[w] && dfs(w))){
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int bipartite_matching(){
int res = 0;
memset(match, -1, sizeof(match));
for(int v = 0; v < V; v++){
if(match[v] < 0){
memset(used, 0, sizeof(used));
if(dfs(v)){
++res;
}
}
}
return res;
}
#define MAX_N 200
int G2, B, M;
int d[MAX_N][MAX_N];
void clear_graph(){
for(int v = 0; v < V; v++) G[v].clear();
}
void solve(){
V = G2 + B;
// 建补图
clear_graph();
for(int i = 0; i < G2; i++){
for(int j = 0; j < B; j++){
if(d[i][j]) add_edge(i, G2 + j);
}
}
// 补图的最大独立集
printf("%d\n", V - bipartite_matching());
}
int main(){
int t = 0;
while(~scanf("%d%d%d", &G2, &B, &M) && (G2 | B | M)){
for(int i = 0; i < G2; i++){
for(int j = 0; j < B; j++){
d[i][j] = 1;
}
}
for(int i = 0; i < M; i++){
int x, y;
scanf("%d%d", &x, &y);
d[x - 1][y - 1] = 0;
}
printf("Case %d: ", ++t);
solve();
}
return 0;
}