POJ1446 Girls and Boys

来源:互联网 发布:大数据框架hadoop 编辑:程序博客网 时间:2024/06/03 19:38

Girls and Boys
Time Limit: 5000MS Memory Limit: 10000KTotal Submissions: 12747 Accepted: 5678

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

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

Source

Southeastern Europe 2000

——————————————————————————————————

题目的意思是给出n个学生喜欢的关系,问最多选出多少个人没有喜欢关系

思路:求最大独立集,最大独立集=点数n-最大匹配数

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int MAXN=1000;int uN,vN;  //u,v数目int g[MAXN][MAXN];//编号是0~n-1的int linker[MAXN];bool used[MAXN];int mat[MAXN];int aa[MAXN];struct area{    int x1,x2,y1,y2;} s[100005];struct point{    int x,y;} p[100005];bool dfs(int u){    int v;    for(v=0; v<vN; v++)        if(g[u][v]&&!used[v])        {            used[v]=true;            if(linker[v]==-1||dfs(linker[v]))            {                linker[v]=u;                return true;            }        }    return false;}int hungary(){    int res=0;    int u;    memset(linker,-1,sizeof(linker));    for(u=0; u<uN; u++)    {        memset(used,0,sizeof(used));        if(dfs(u))  res++;    }    return res;}int main(){    int n,m,x,y;    int cas=1;    while(~scanf("%d",&n))    {        memset(g,0,sizeof g);        for(int i=0; i<n; i++)        {            scanf("%d: (%d)",&x,&m);            for(int i=0; i<m; i++)            {                scanf("%d",&y);                g[x][y]=1;            }        }        uN=vN=n;        printf("%d\n",n-hungary()/2);    }    return 0;}



原创粉丝点击