【hdu 1068 基础二分图】Girls and Boys

来源:互联网 发布:小学生裙子淘宝 编辑:程序博客网 时间:2024/05/14 19:42

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11527    Accepted Submission(s): 5412



Problem Description
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.

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, for n subjects.
For each given data set, the program should write to standard output a line containing the result.
 

Sample Input
70: (3) 4 5 61: (2) 4 62: (0)3: (0)4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0
 

Sample Output
52
 
ac代码
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <list>#include <bitset>#include <functional>#include <utility>//pair#include <iomanip>using namespace std;#define MAX 1005int n,m;int match[MAX];bool visit[MAX],p[MAX][MAX];//模板开始bool dfs(int k){    int i,t;    for(i=0; i<m; i++)    {        if(p[k][i] && !visit[i])        {            visit[i] = true;            t = match[i]; match[i] = k;            if(t == -1 || dfs(t)) return true;            match[i] = t;        }       }       return false;}   int Max_match(){    int ans = 0;    memset(match,-1,sizeof(match));    for(int i=0; i<n; i++)    {        memset(visit,0,sizeof(visit));        if(dfs(i)) ans++;    }       return ans;}   //模板结束int main(){    int t,i,a,b,c;    while(scanf("%d",&t)!=EOF)    {        n = t; m = t;        memset(p,0,sizeof(p));        while(t--)        {            scanf("%d: (%d)",&a,&b);            for(i=0;i<b;i++)            {                scanf("%d",&c);                p[a][c] = 1;            }           }          printf("%d\n",n - Max_match()/2);        }        return 0;}


1 0
原创粉丝点击