hdu1068Girls and Boys

来源:互联网 发布:禁止修改ip软件 编辑:程序博客网 时间:2024/05/17 06:38

Girls and Boys

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



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
 

Source
Southeastern Europe 2000

题目中由于没有说明男女,所以建图时会有1-2,也会有2-1,
所以求出来的匹配数要2,接下来便是一个最大独立子集问题
#include<bits/stdc++.h>const int MAXN=1010;const int MAXM=2010;struct Edge{    int to,next;}edge[MAXM];int tot;bool used[MAXN];int linker[MAXN];int head[MAXN];void init(){    tot=0;    memset(head,-1,sizeof(head));}void addedge(int u,int v){    edge[tot].to=v;    edge[tot].next=head[u];    head[u]=tot++;}bool dfs(int u){    for(int i=head[u];i!=-1;i=edge[i].next){        int v=edge[i].to;        if(!used[v]){            used[v]=true;            if(linker[v]==-1||dfs(linker[v])){                linker[v]=u;                return true;            }        }    }    return false;}int hungary(int n){    memset(linker,-1,sizeof(linker));    int ret=0;    for(int u=0;u<n;u++){        memset(used,false,sizeof(used));        if(dfs(u))            ret++;    }    //for(int i=0;i<n;i++)        //printf("%d %d\n",i,linker[i]);    return ret;}int main(){    int k,n,m;    int u,v;    while(scanf("%d%",&n)!=EOF){        init();        for(int i=0;i<n;i++){            scanf("%d: (%d)",&u,&k);            while(k--){                scanf("%d",&v);                addedge(u,v);            }        }        printf("%d\n",n-hungary(n)/2);    }    return 0;}


 
0 0