HDU1068Girls and Boys二分图最大匹配

来源:互联网 发布:sql语句单引号转义 编辑:程序博客网 时间:2024/04/30 03:10

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!!!因为重复加边了~~好好处理括号,真是过分,搞ACM大半年,回回碰到字符串就卡壳
#include<cstdio>#include<cstring>using namespace std;/* **************************************************************************//二分图匹配(匈牙利算法的DFS实现)//初始化:g[][]两边顶点的划分情况//建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配//g没有边相连则初始化为0//uN是匹配左边的顶点数,vN是匹配右边的顶点数//调用:res=hungary();输出最大匹配数//优点:适用于稠密图,DFS找增广路,实现简洁易于理解//时间复杂度:O(VE)//***************************************************************************///顶点编号从0开始的const int MAXN=500;int uN,vN;//u,v数目int g[MAXN][MAXN];int linker[MAXN];bool used[MAXN];int k,m,n,u,v;bool dfs(int u)//从左边开始找增广路径{    int v;    for(v=1;v<=n;v++)//这个顶点编号从0开始,若要从1开始需要修改      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=1;u<=n;u++)    {        memset(used,0,sizeof(used));        if(dfs(u)) res++;    }    return res;}//******************************************************************************/int main(){    //freopen("cin.txt","r",stdin);    while(~scanf("%d",&n))    {        memset(g,0,sizeof(g));        for(int i=1;i<=n;i++)        {            scanf("%d: (%d)",&k,&u);///           // printf("%d  ",u);            while(u--)            {                scanf("%d",&v);                //printf("%d  ",v);                g[i][v+1]=1;            }         //   puts("");        }       // memset(linker,-1,sizeof(linker));        printf("%d\n",n-hungary()/2);    }    return 0;}



0 0
原创粉丝点击