POJ 3692 补图的最大独立集

题意

传送门 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值