Girls and Boys HDU (二分匹配入门题)

                                    Girls and Boys

                                           T ime Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                           T otal Submission(s): 5722    Accepted Submission(s): 2561


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
  
  
7 0: (3) 4 5 6 1: (2) 4 6 2: (0) 3: (0) 4: (2) 0 1 5: (1) 0 6: (2) 0 1 3 0: (2) 1 2 1: (1) 0 2: (1) 0
 

Sample Output
5
2
 
题意:
     
      一题二分匹配求解最大独立集的入门题。 每次都被英语水平坑到底啊!!!!!!
     题目看了半天。题目就是最大独立集的定义。就是给你男生女生数量叫你求出在这些的人中,能够使他们不认识到最大集合。
    显然我们是不知道输入的人到底是男还是女,于是我们就把某一个人拆成2个计算。
   然后根据公式::
                二分图最大独立集合 = 节点数 - 最大匹配数
  但最后我们要把结果除以二,因为我们拆成了两个人计算,即:在计算过程中是不分男女的进行查找。
Ok,下面给出用两个代码,一个是用矩阵写的,用时多,而且写得有点搓。第二个是用邻接表写得,查找快速。
 
 
邻接矩阵:
 
#include <stdio.h>
#include <string.h>
#define CL(x,v);memset(x,v,sizeof(x));

const int maxn = 1002;
int num[maxn];
bool used[maxn],map[maxn][maxn];
int n;

bool Find(int u)
{
    for(int j = 0;j < n;j++)
      if(!used[j]&&map[u][j])
      {
          used[j] = true;
          if(num[j]==-1||Find(num[j]))
          {
              num[j] = u;
              return true;
          }
      }
    return false;
}
int Hungry()
{
    int res = 0;
    CL(num,-1);
    for(int i = 0;i < n;i++)
    {
        CL(used,0);
        if(Find(i)) res++;
    }
    return res;
}
int main()
{
    int m,i,j,a,b;
    while(~scanf("%d",&n))
    {
        CL(map,0);
        for(i = 0;i < n;i++)
        {
            scanf("%d: (%d)",&a,&m);  //This is a good idea.
            for(j = 0;j < m;j++)
            {
               scanf("%d",&b);
               map[a][b] = true;
            }
        }
        int ans = Hungry();
        printf("%d\n",n-ans/2);
    }
    return 0;
}

 
邻接表:
  
 
#include <stdio.h>
#include <string.h>
#define CL(x,v);memset(x,v,sizeof(x));

const int maxn = 1000;
int n,top = 0;
bool used[maxn];
int link[maxn];
int head[maxn*maxn],next[maxn*maxn],num[maxn*maxn];

void Add(int u,int v)
{
    next[top] = head[u];
    num[top] = v;
    head[u] = top++;
}

bool Find(int u)
{
    for(int v = head[u];v != -1;v = next[v])
      if(!used[num[v]])
      {
          used[num[v]] = true;
          if(link[num[v]] == -1||Find(link[num[v]]))
          {
              link[num[v]] = u;
              return true;
          }
      }
    return false;
}

int Hungry()
{
    int res = 0;
    CL(link,-1);
    for(int i = 0;i < n;i++)
    {
        CL(used,0);
        if(Find(i))
          res++;
    }
    return res;
}
int main()
{
    int i,j,m,a,b;
    while(~scanf("%d",&n))
    {
        top = 0;
        CL(head,-1);
        for(i = 0;i < n;i++)
        {
            scanf("%d: (%d)",&a,&m);
            for(j = 0;j < m;j++)
            {
                scanf("%d",&b);
                Add(a,b);
            }
        }
        int ans = Hungry();
        printf("%d\n",n - ans/2);
    }
    return 0;
}

 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值