hduoj-----(1068)Girls and Boys(二分匹配)
Girls and Boys
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7577 Accepted Submission(s): 3472
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.
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
2
题意大致为: 有一个学校,男生女生要搭配,然后排除男神和男生搞基,女生和女生玩拉拉的意思,问最少有多少个落单的倒霉求?
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=;
int n,a,b,c;
bool mat[maxn][maxn];
bool vis[maxn];
int girl[maxn];
bool check(int x){
for(int i=;i<n;i++){
if(mat[x][i]==&&!vis[i]){
vis[i]=;
if(girl[i]==-||check(girl[i])){
girl[i]=x;
return ;
}
}
}
return ;
}
int main()
{
//freopen("test.in","r",stdin);
while(scanf("%d",&n)!=EOF){
memset(mat,,sizeof(mat));
memset(girl,-,sizeof(girl));
for(int i=;i<n;i++){
scanf("%d: (%d)",&a,&b);
while(b--){
scanf("%d",&c);
mat[a][c]=;
}
}
int ans=;
for(int j=;j<n;j++){
memset(vis,,sizeof(vis));
if(check(j))ans++;
}
/*通过最大二分匹配,我们得到了最大匹配数,但是由于男生女生
都算了一遍,所以是不是就得除以二。这样就是最大匹配数了*/
printf("%d\n",n-ans/);
}
return ;
}
最新文章
- ajax的post方式和get方式比较,以及需要注意的地方
- php翻页
- js的动态加载、缓存、更新以及复用(一)
- 学期总结ngu
- android webview删除缓存
- acdream 1685 多民族王国(DFS,并查集)
- How to get cocoapods work on Yosemite
- Matlab实现ItemBasedCF算法
- 【C语言】37个关键字
- Android 的 Relative Layout 常量
- cocos2dx进阶学习之CCLayer
- HSQLDB相关信息及用法汇总
- 基于Kafka Connect框架DataPipeline在实时数据集成上做了哪些提升?
- php禁止某ip或ip地址段访问的方法(转载)
- 子级div相对于父级div位置不变
- Confluence 6 查看所有空间
- requestAnimationFrame结束demo
- 860. Lemonade Change
- POJ 2845
- thymeleaf-extras-shiro