版权声明:本文作者靖心。靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载。 https://blog.csdn.net/kenden23/article/details/32696867

选择出一组学生。这组学生里面不能彼此之间有过恋爱史的。

又是一个典型的二分图问题。

只是须要把全部学生看成一组*2。然后求最大匹配,然后除以2. 这样事实上建图的时候。建成有向图也是能够的了。并且也是给出了两个方向的点了。

注意本题没有给出最大数是多少学生了,所以最好使用动态分配内存了。

并且本题的输入处理也特别点。要处理好,用好scanf这个函数。

#include <stdio.h>
#include <stdlib.h> bool **stus, *used;
int *linker;
int N; void initGraph()
{
stus = (bool **) malloc(N * sizeof(bool *));
for (int i = 1; i < N; i++)
stus[i] = (bool *) calloc(N, sizeof(bool));
}
void freeGraph()
{
for (int i = 1; i < N; i++)
free(stus[i]);
free(stus);
} bool hunDFS(int u)
{
for (int v = 1; v < N; v++)
{
if (!used[v] && stus[u][v])
{
used[v] = true;
if (!linker[v] || hunDFS(linker[v]))
{
linker[v] = u;
return true;
}
}
}
return false;
} int hungary()
{
int ans = 0;
linker = (int *) calloc(N, sizeof(int));
for (int i = 1; i < N; i++)
{
used = (bool *) calloc(N, sizeof(bool));
if (hunDFS(i)) ans++;
free(used);
}
free(linker);
return ans>>1;
} int main()
{
int k, v;
while (scanf("%d", &N) != EOF)
{
N++;
initGraph();
for (int u = 1; u < N; u++)
{
scanf("%d: (%d)", &v, &k);
for (int i = 0; i < k; i++)
{
scanf("%d", &v);
stus[u][v+1] = stus[v+1][u] = true;
}
}
printf("%d\n", N - 1 - hungary());
freeGraph();
}
return 0;
}

HDU 1068 Girls And Boys 二分图题解的更多相关文章

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

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 1068 Girls and Boys (二分图最大独立集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 有n个同学,格式ni:(m) n1 n2 n3表示同学ni有缘与n1,n2,n3成为情侣,求集合 ...

  3. [HDU] 1068 Girls and Boys(二分图最大匹配)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1068 本题求二分图最大独立点集.因为最大独立点集=顶点数-最大匹配数.所以转化为求最大匹配.因为没有给 ...

  4. hdu 1068 Girls and Boys 二分图的最大匹配

    题目链接:pid=1068">http://acm.hdu.edu.cn/showproblem.php? pid=1068 #include <iostream> #in ...

  5. hdu - 1068 Girls and Boys (二分图最大独立集+拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) ...

  6. (step6.3.2)hdu 1068(Girls and Boys——二分图的最大独立集)

    题目大意:第一行输入一个整数n,表示有n个节点.在接下来的n行中,每行的输入数据的格式是: 1: (2) 4 6 :表示编号为1的人认识2个人,他们分别是4.6: 求,最多能找到多少个人,他们互不认识 ...

  7. hdu 1068 Girls and Boys(匈牙利算法求最大独立集)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDU——1068 Girls and Boys

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数)

    HDU 1068 :题目链接 题意:一些男孩和女孩,给出一些人物关系,然后问能找到最多有多少个人都互不认识. 转换一下:就是大家都不认识的人,即最大独立集合 #include <iostream ...

随机推荐

  1. git切换分支(自记)

    git fetch git checkout feature/A4-page

  2. iOS UIImage:获取图片主色调

    本文转载至 http://www.wahenzan.com/a/mdev/ios/2015/0325/1677.html -(UIColor*)mostColor{ #if __IPHONE_OS_V ...

  3. Mybatis输入输出映射

    一.输入映射 1.传递简单类型 <select id="findUserById" parameterType="int" resultType=&quo ...

  4. 在PowerDesigner中自动生成sqlserver字段备注

    在PowerDesigner中自动生成sqlserver字段备注 PowerDesigner是数据库设计人员常用的设计工具,但其自生默认生成的代码并不会生成sqlserver数据库的字段备注说明.在生 ...

  5. Elasticsearch学习之深入搜索五 --- phrase matching搜索技术

    1. 近似匹配 什么是近似匹配,两个句子 java is my favourite programming language, and I also think spark is a very goo ...

  6. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  7. RDMA卡的检测方法

    1. udaddy This script covers RDMA_CM UD connections. (It establishes a set of unreliable RDMA datagr ...

  8. Scrapy计划表

    第一步 Scrapy 一览:理解Scrapy是什么,他能帮到你什么 安装指南:在电脑上安装Scrapy Scrapy 教程:编写第一个Scrapy项目 示例:通过前人写好的Scrapy项目进行学习 基 ...

  9. 自定义容器启动脚本报错:exec user process caused "no such file or directory"

    创建容器起不来,一直是restarting状态,查看容器的报错日志如下: standard_init_linux.go:178: exec user process caused "no s ...

  10. 【咸鱼教程】protobuf在websocket通讯中的使用

    教程目录一 protobuf简介二 使用protobuf三 Demo下载 参考: CSDN:Egret项目中使用protobuf(protobufjs) TS项目中使用Protobuf的解决方案(ba ...