hdu1068——Girls and Boys

来源:互联网 发布:人工智能的创造力 编辑:程序博客网 时间:2024/06/07 15:23
题目链接:打开此题目
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

方法:最大独立集、最大匹配

思想:

最大独立集指的是两两之间没有边的顶点的集合,顶点最多的独立集成
为最大独立集。二分图的最大独立集=节点数-(减号)最大匹配数。
由于本题是要找出最大的没有关系的集合,即最大独立集。而求最大独立集重点在于求最大匹配数,
本题中给出的是同学之间的亲密关系,并没有指出哪些是男哪些是女,所以求出的最大匹配数
要除以2才是真正的匹配数。
匈牙利算法:点我这里

方法:最大独立集、最大匹配


1.vector(249ms)

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>using namespace std;#define N 505bool vis[N];int n,match[N];vector<int>q[N];bool dfs(int u){    int v;    for(v=0; v<q[u].size(); v++)    {        if(!vis[q[u][v]])        {            vis[q[u][v]]=1;            if(match[q[u][v]]==-1||dfs(match[q[u][v]]))            {                match[q[u][v]]=u;                return 1;            }        }    }    return 0;}int main(){    int i,a,m,b;    char c1,c2,c;    while(~scanf("%d",&n))    {        for(i=0; i<n; i++)            q[i].clear();        int nn=n;        while(nn--)        {            scanf("%d%c %c%d%c",&a,&c,&c1,&m,&c2);            while(m--)            {                scanf("%d",&b);                q[a].push_back(b);            }        }        memset(match,-1,sizeof(match));        int ans=0;        for(i=0; i<n; i++)        {            memset(vis,0,sizeof(vis));            if(dfs(i)) ans++;        }        printf("%d\n",(n*2-ans)/2);    }    return 0;}

2.链式向前星(390ms)

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;#define N 505struct Edge{    int next,to;} q[N*N];bool vis[N];int head[N],cnt,match[N];void add_edge(int u,int v){    q[cnt].to=v,q[cnt].next=head[u],head[u]=cnt++;}bool dfs(int u){    int i;    for(i=head[u]; ~i; i=q[i].next)    {        int v=q[i].to;        if(!vis[v])        {            vis[v]=1;            if(match[v]==-1||dfs(match[v]))            {                match[v]=u;                return 1;            }        }    }    return 0;}int main(){    int n,i,a,m,b;    char c1,c2,c;    while(~scanf("%d",&n))    {        memset(head,-1,sizeof(head));        int nn=n;        cnt=0;        while(nn--)        {            scanf("%d%c %c%d%c",&a,&c,&c1,&m,&c2);            while(m--)            {                scanf("%d",&b);                add_edge(a,b);            }        }        memset(match,-1,sizeof(match));        int ans=0;        for(i=0; i<n; i++)        {            memset(vis,0,sizeof(vis));            if(dfs(i)) ans++;        }        printf("%d\n",(n*2-ans)/2);    }    return 0;}

3.二维数组(2481ms)

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;#define N 505bool vis[N];int mp[N][N],n,match[N];bool dfs(int u){    int v;    for(v=0;v<n;v++)    {        if(!vis[v]&&mp[u][v]==1)        {            vis[v]=1;            if(match[v]==-1||dfs(match[v]))            {                match[v]=u;                return 1;            }        }    }    return 0;}int main(){    int i,a,m,b,j;    char c1,c2,c;    while(~scanf("%d",&n))    {        for(i=0; i<n; i++)            for(j=0; j<n; j++)                mp[i][j]=0;        int nn=n;        while(nn--)        {            scanf("%d%c %c%d%c",&a,&c,&c1,&m,&c2);            while(m--)            {                scanf("%d",&b);                mp[a][b]=1;            }        }        memset(match,-1,sizeof(match));        int ans=0;        for(i=0; i<n; i++)        {            memset(vis,0,sizeof(vis));            if(dfs(i)) ans++;        }        printf("%d\n",(n*2-ans)/2);    }    return 0;}

原创粉丝点击