标签:res clu cond ++ find ber ica define amp
Input
7 0: (3) 4 5 6 1: (2) 4 6 2: (0) 3: (0) 4: (2) 0 1 5: (1) 0 6: (2) 0 1 3 0: (2) 1 2 1: (1) 0 2: (1) 0
Output
5 2
Sample Input
7 0: (3) 4 5 6 1: (2) 4 6 2: (0) 3: (0) 4: (2) 0 1 5: (1) 0 6: (2) 0 1 3 0: (2) 1 2 1: (1) 0 2: (1) 0
Sample Output
5 2
题意:输入n,再输入n行数字,每行第一个数字为学生编号,第二个数字为匹配学生的总数k,再输入k个匹配学生编号,问最小的没有匹配的学生数为多少?
思路:这是一个求最大独立集的问题。最大独立集 = 顶点个数-最小顶点覆盖(最大匹配)。由于题目只给了匹配学生编号,没有分性别给出,所以一定会有重复,比如样例1中0和4匹配了两次,所以我们求出的“最大匹配”除以2后才是实际的最大匹配,去重。
#include<stdio.h> #include<string.h> #define N 1000 int n,m; int e[N][N]; int book[N],match[N]; int dfs(int u) { int i; for(i = 0; i < n; i ++) { if(!book[i]&&e[u][i]) { book[i] = 1; if(!match[i]||dfs(match[i])) { match[i] = u; //match[u] = i; return 1; } } } return 0; } int main() { int i,j,t1,t2,sum,d; char a,b,c,f; while(scanf("%d",&n)!=EOF) { memset(e,0,sizeof(e)); memset(match,0,sizeof(match)); sum = 0; for(i = 0; i < n; i ++) { scanf("%d%c%c",&t1,&a,&f);//printf("t1=%d\n",t1); scanf("%c%d%c",&b,&m,&c);//printf("m=%d\n",m); for(j = 0; j < m; j ++) { scanf("%d",&t2);//printf("t2=%d\n",t2); e[t1][t2] = 1; //e[t2][t1] = 1; } } for(i = 0; i < n; i ++) { memset(book,0,sizeof(book)); if(dfs(i)) sum ++; } printf("%d\n",n-sum/2); } return 0; }
【二分图匹配入门专题1】B - Girls and Boys hdu1068【最大独立集】
标签:res clu cond ++ find ber ica define amp
原文地址:http://www.cnblogs.com/chengdongni/p/7350503.html