您的位置:首页 > 其它

zoj 1137 Girls and Boys 二分图的最大独立集

2013-08-25 09:03 288 查看
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define SIZE 505

int n;
int graph[SIZE][SIZE];
int visited[SIZE];
int match[SIZE];

int Hungary(int u){
	int i;
	for(i = 0; i < n; i++){
		if(!visited[i] && graph[u][i]){
			visited[i] = 1;
			if(match[i] == -1 || Hungary(match[i])){
				match[i] = u;
				return 1;
			}
		}
	}
	return 0;
}

int main(){
	//freopen("in.txt", "r", stdin);
	while(cin>>n){
		int i, j;
		memset(graph, 0, sizeof(graph));
		memset(match, -1, sizeof(match));
		for(i = 0; i < n; i++){
			int stu, knowN, tmp_know;
			scanf("%d: (%d)", &stu, &knowN);
			for(j = 0; j < knowN; j++){
				cin>>tmp_know;
				graph[i][tmp_know] = 1;
			}
		}
		int ans = 0;
		for(i = 0; i < n; i++){
			memset(visited, 0, sizeof(visited));
			if(Hungary(i))
				ans++;
		}
		cout<<n - ans / 2<<endl;	//除2很关键,因为算了两遍
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: