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

来源:互联网 发布:动画人物设计软件 编辑:程序博客网 时间:2024/06/07 01:32
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define SIZE 505int 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;}

原创粉丝点击