HDU 1068 Girls and Boys 二分图最大独立集(最大二分匹配)

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

【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】
: ()
: ()
: ()
: ()
: ()
: ()
: () : ()
: ()
: ()

【Sample Output】


【题意】

题目给定一些男女生之间相互的romantic关系,要求找出一个最大的集合,使得该集合中的所有男女生之间都不存在romantic关系。

【分析】

一个二分图的最大独立集点数与最大二分匹配个数有直接的关系:

最大独立集点数 = 顶点数 - 最大二分匹配对数

故本题直接转化为求最大二分匹配即可,需要注意的是,题中给出的条件是1指向2,2也会指向1,所以最终算出来的匹配数其实是实际对数的两倍,最终被顶点数减去之前首先需要折半。

基础二分匹配练手题。

 /*
ID: Chen Fan
PROG: hdu1068
LANG: G++
*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; typedef struct nod
{ int a,b;
} node;
node a[];
int result[],start[],num[];
bool flag[]; bool op(node a,node b)
{
if (a.a==b.a) return a.b<b.b;
else return a.a<b.a;
} bool find(int s)
{
for (int i=;i<num[s];i++)
{
int now=a[start[s]+i].b;
if (!flag[now])
{
flag[now]=true;
if (result[now]==-||find(result[now]))
{
result[now]=s;
return true;
}
}
}
return false;
} int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
int tail=;
for (int i=;i<=n;i++)
{
int x,p;
scanf("%d: (%d",&x,&p);
getchar();
for (int j=;j<=p;j++)
{
int y;
scanf("%d",&y);
tail++;
a[tail].a=x;
a[tail].b=y;
}
}
sort(&a[],&a[tail+],op);
int o=-;
memset(num,,sizeof(num));
for (int i=;i<=tail;i++)
{
if (o!=a[i].a)
{
o=a[i].a;
start[o]=i;
}
num[o]++;
} memset(result,-,sizeof(result));
int ans=;
for (int i=;i<n;i++)
{
memset(flag,,sizeof(flag));
if (find(i)) ans++;
} printf("%d\n",n-ans/);
} return ;
}
上一篇:pod install报错 [!] Error installing......


下一篇:"undefined method `root' for nil:NilClass" error when using "pod install" 解决办法