本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配。

究竟怎样学习一种新算法呢?

我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己直接看代码,竟然不到半个小时看懂了。然后就能够直接拿来解题啦。

比方topcoder上有这个算法的非常具体的分析。真没看懂。

代码竟然比分析更清晰了?我也不好下结论。

可是我认为基本的思想还是有作用的。

说说我对这个算法的理解吧:

1 假设二分图分为两个集合 U, V,那么从一个集合U出发

2 U的一个点u连线到V的一个点v,假设v点没有别的点连接,那么就算连接成功了

3 假设v点有别的点u2连接了。那么就u2就须要让u连接,然后u2递归寻找别的路径点去连接。假设寻找成功找到v2。那么u2就连接v2;

4 假设u2没有找到别的点连接,那么u2就不让u了,所以u就不能连接v了。

5 那么u就继续寻找别的点连接,反复上述过程,直到u没找到别的点连接。那么u就是连接失败。继续下一个点查找。

就是一个递归查找的过程。

这位大牛的分析。我也是没看懂:https://www.byvoid.com/blog/hungary

可是能够參考他的图。然后依照我上面说的方法连接以下的图。保证你懂了:

本题C++程序:

bool isBipartite_2(vector<vector<bool> > & stus, int src)
{
vector<int> colors(stus.size(), -1);
colors[src] = 0;
queue<int> qu;
qu.push(src);
while (qu.size())
{
int u = qu.front();
qu.pop();
for (int v = 0; v < (int)stus.size(); v++)
{
if (stus[u][v] && colors[v] == -1)
{
colors[v] = 1 - colors[u];
qu.push(v);
}
else if (stus[u][v] && colors[v] == colors[u]) return false;
}
}
return true;
} bool hunDfs_2(vector<vector<bool> > &stus, vector<bool> &used,
vector<int> &linker, int src)
{
for (int d = 0; d < (int)stus.size(); d++)
{
if (stus[src][d] && !used[d])
{
used[d] = true;
if (linker[d] == -1 ||
hunDfs_2(stus, used, linker, linker[d]))
{
linker[d] = src;
return true;
}
}
}
return false;
} int hungary_2(vector<vector<bool> > &stus)
{
int res = 0;
vector<int> linker(stus.size(), -1);
for (int i = 0; i < (int)stus.size(); i++)
{
vector<bool> used(stus.size(), false);
if (hunDfs_2(stus, used, linker, i)) res++;
}
return res;
} int main()
{
int n, m, u, v;
while (scanf("%d %d", &n, &m) != EOF)
{
vector<vector<bool> > stus(n, vector<bool>(n, 0));
while (m--)
{
scanf("%d %d", &u, &v);
stus[u-1][v-1] = stus[v-1][u-1] = true;//记得:这是个无向图,否则错
}
if (isBipartite_2(stus, 0))
{
printf("%d\n", hungary_2(stus)>>1);
}
else puts("No");
}
return 0;
}

HDU 2444 The Accomodation of Students二分图判定和匈牙利算法的更多相关文章

  1. HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)

    题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...

  2. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  3. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  4. HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)

    There are a group of students. Some of them may know each other, while others don't. For example, A ...

  5. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  6. HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

    这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

  7. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  9. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

随机推荐

  1. 使用sed进行文字替换

    范式: sed -i "s/查找内容/替换后内容/g" `grep 查找内容 -rl 查找开始路径` 例子: #sed -i "s/abc/ABC/g" `gr ...

  2. Spring+DBUnit+H2----项目单元测试

    http://yugouai.iteye.com/blog/1879337 今天够郁闷的,早上调好的代码,到中午调试不同了,分析不出问题,H2的JDBC报错:org.h2.jdbc.JdbcSQLEx ...

  3. TextView跑步灯效果及在特殊情况下无效的解决方式

    概述: 关于在TextView中使用跑马灯效果的样例在网上一搜一大把.他们可能会让你像以下这样来在xml中定义TextView控件的属性.而事实也确是如此. 只是我不知道他们有没有遇到和我一样的问题( ...

  4. Parse 使用- iOS 后台数据[转]

    原文地址:http://blog.csdn.net/vipwangl/article/details/8846415 最近在学习Parse,但是Parse的中文教程比较少,看到这篇英文教程,把它翻译一 ...

  5. PyQt5教程——组件 Ⅱ(八)

    这部分的教程将会继续介绍PyQt5的组件.我们这节教程的内容将包括像素图(QPixmap),单行文本框(QLineEdit)和下拉列表框(QComboBox) 像素图(QPixmap) 像素图(QPi ...

  6. 算法笔记_189:历届试题 横向打印二叉树(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 二叉树可以用于排序.其原理很简单:对于一个排序二叉树添加新节点时,先与根节点比较,若小则交给左子树继续处理,否则交给右子树. 当遇到空子树 ...

  7. 【Oracle】RAC 10.2.0.1升级10.2.0.5

    环境: OS:OEL5.6 RAC:10.2.0.1.0 相关环境变量: CRS_HOME /u01/app/oracle/product/10.2.0/db_1 ORACLE_HOME   /u01 ...

  8. java String字符串

      五.java数据类型之String(字符串) CreateTime--2017年7月21日16:17:45 Author:Marydon (一)数据格式 (二)初始化 // 方式一 String ...

  9. 〖Linux〗VIM youcompleteme 自动补全 #include 文件名称

    1. 拷贝配置文件 cp ~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py ~/.vim/.ycm_extra_conf.py 2. 修改配 ...

  10. Highcharts网页版

    //后台控制器中(SpringMVC) @RequestMapping(value="/getAll",method=RequestMethod.POST) @ResponseBo ...