由konig定理 最小点覆盖==最大匹配
可以发现的是在一个二分图中只要将最小点覆盖集中的点都去掉,那么剩下的点之间就没有边了, 也就是剩下来的为最大点独立集
然后就可以用 (最大点独立集)==(n-最小点覆盖)==(n-最大匹配)
Girls and Boys
Description In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been "romantically involved". The result of the program is the number of students in such a set.
Input The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:
the number of students the description of each student, in the following format student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ... or student_identifier:(0) The student_identifier is an integer number between 0 and n-1 (n <=500 ), for n subjects. Output For each given data set, the program should write to standard output a line containing the result.
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 Source |
#include <stdio.h> #include <string.h> #define N 1010 int n; int g[505][505]; int cnt,pre[N]; int mark[N],frt[N]; int save1[N],save2[N]; int cnt1,cnt2; void fdfs(int s,int f) { if(f==0) save1[cnt1++]=s; else save2[cnt2++]=s; mark[s]=f; for(int i=0;i<n;i++) { if( g[s][i]==0 ) continue; if( mark[i]==-1 ) fdfs(i,f^1); } } int dfs(int s) { for(int i=0;i<cnt2;i++) { if(g[ save1[s] ][ save2[i] ]==0||mark[i]==1) continue; mark[i]=1; if(frt[i]==-1||dfs(frt[i])) { frt[i]=s; return 1; } } return 0; } int main() { int ans; while( scanf("%d",&n) != EOF ) { cnt1=cnt2=0; ans=0; memset(g,0,sizeof(g)); for(int i=0;i<n;i++) { int x,y; scanf("%d: (%d)",&x,&y); //if(y==0) ans++; for(int j=0;j<y;j++) { int tmp; scanf("%d",&tmp); g[i][tmp]=g[tmp][i]=1; } } memset(mark,-1,sizeof(mark)); for(int i=0;i<n;i++) { if(mark[i]!=-1) continue; fdfs(i,0); } int sum=0; memset(frt,-1,sizeof(frt)); for(int i=0;i<cnt1;i++) { memset(mark,0,sizeof(mark)); sum += dfs(i); } printf("%d\n",n-sum); } return 0; }