Poj 3692 二分图最大团

 题目链接:http://poj.org/problem?id=3692

 

最大团=补图最大独立集

二分图最大独立集=全集-最小路径覆盖集

最小路径覆盖集=最大匹配

 

匈牙利算法求最大匹配

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 256
int map[M][M];
int tmp[M][M];
int ls[M];
int uboys[M];
int vis[M];
int g;
int girls,boys,m;
int Try(int girl)
{
	int boy;
	for(boy=1;boy<=boys;boy++){
		if(map[girl][boy]&&vis[boy]!=g){
			vis[boy]=g;
			if(!uboys[boy]||Try(uboys[boy])){
				uboys[boy]=girl;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int i,j,ans;
	int t=1;
	while(scanf("%d%d%d",&girls,&boys,&m),girls|boys|m){
		memset(ls,0,sizeof(ls));
		memset(map,1,sizeof(map));
		while(m--){
			scanf("%d%d",&i,&j);
			map[i][j]=0;
		}
		memset(vis,0,sizeof(vis));
		memset(uboys,0,sizeof(uboys));
		for(ans=0,g=1;g<=girls;g++)
			ans+=Try(g);
		printf("Case %d: %d\n",t++,boys+girls-ans);
	}
	return 0;
}

你可能感兴趣的:(Poj 3692 二分图最大团)