hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3565 Accepted Submission(s): 1659Problem DescriptionThere
are a group of students. Some of them may know each other, while others
don't. For example, A and B know each other, B and C know each other.
But this may not imply that A and C know each other.Now you are
given all pairs of students who know each other. Your task is to divide
the students into two groups so that any two students in the same group
don't know each other.If this goal can be achieved, then arrange them
into double rooms. Remember, only paris appearing in the previous given
set can live in the same room, which means only known students can live
in the same room.Calculate the maximum number of pairs that can be arranged into these double rooms.
InputFor each data set:
The
first line gives two integers, n and m(1<n<=200), indicating
there are n students and m pairs of students who know each other. The
next m lines give such pairs.Proceed to the end of file.
OutputIf
these students cannot be divided into two groups, print "No".
Otherwise, print the maximum number of pairs that can be arranged in
those rooms.Sample Input4 41 21 31 42 36 51 21 31 42 53 6Sample OutputNo3Source
题意: 有n个人,m条关系,问通过这些关系判断,所有人是否可以分成两个组,如果可以則求出一组最多多少人。
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include <iostream>
- #include <algorithm>
- #include <climits>
- #include <queue>
- #define ll long long
- using namespace std;
- const int N = ;
- int head[N],total,visit[N];
- int link[N],color[];
- struct nodes
- {
- int e,next;
- } Edge[N];
- void add(int x,int y)//加边,邻接表形式建图
- {
- Edge[total].e = y;
- Edge[total].next = head[x];
- head[x] = total++;
- }
- int dfs(int f) //匈牙利算法中的二分匹配dfs实现
- {
- for(int i = head[f]; i != -; i = Edge[i].next)//邻接表遍历与起始点f相连的所有点
- {
- int s = Edge[i].e; //与起始点相连的点
- if(visit[s]) continue;//访问过就跳过
- visit[s] = ;
- if(link[s] == - || dfs(link[s]))//寻找增广路径
- {
- link[s] = f ;
- return ;
- }
- }
- return ;
- }
- void init()//初始化
- {
- total = ;
- memset(head,-,sizeof(head));
- memset(link,-,sizeof(link));
- }
- bool bcolor(int u,int x)
- {
- if(color[u] != -)//如果从上个点出发过来,发现该点已染色
- {
- if(color[u] == x)//如果此点颜色与邻点相同
- return false; //不是二分图,返回染色失败
- }
- else
- {
- color[u] = x^;//该点未染色,則染与上个点不同的颜色
- for(int i = head[u]; i != -; i = Edge[i].next)//遍历与该点相连的所有边
- {
- if(!bcolor(Edge[i].e,color[u]))//如果下一层染色失败,則返回失败
- return false;
- }
- }
- return true;
- }
- bool judge(int n)
- {
- memset(color,-,sizeof(color));//先初始化所有点为-1,代表未染色
- for(int i = ; i <= n; i++)//依次检查点是否染过色
- {
- if(color[i] == -)//若没访问过,則开始填色
- {
- if(!bcolor(i,))
- return false;
- }
- }
- return true;
- }
- int main(void)
- {
- int n,i,cnt,m;
- while(scanf("%d",&n) != -)
- {
- init();
- scanf("%d",&m);
- for(i = ; i < m; i++)
- {
- int x,y;
- scanf("%d %d",&x,&y);
- add(x,y);
- //add(y,x);
- }
- if(!judge(n)) // 判断是否为二分图
- {
- printf("No\n");
- }
- else //匈牙利算法
- {
- for(cnt = ,i = ; i <= n; i++)
- {
- memset(visit,,sizeof(visit));
- if(dfs(i))
- cnt++;
- }
- printf("%d\n",cnt);//若建的双向边則最大匹配数统计了两次,因为是双向的,所以需除以2 ;若建的单向边,則不需要除以2.
- }
- }
- return ;
- }
hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)的更多相关文章
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))
Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students
http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] #include<iostream> #include<cstdio&g ...
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
[题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU——2444 The Accomodation of Students
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- Reflections框架,类扫描工具
Reflections是一个能提供一站式服务的对象. 巧用Reflections库实现包扫描(扫描某个包中某个接口实现.注解等) 它扫描工程的classpath,为元数据建索引,允许你运行时查询元数据 ...
- Nginx报错汇总
1. Nginx 无法启动解决方法 在查看到 logs 中报了如下错误时: 0.0.0.0:80 failed (10013: An attempt was made to access a ...
- mongoDB可视化工具RoBo 3T的安装和使用
第一步下载RoBo3T https://robomongo.org/download 第二步安装 双击安装包安装,修改安装路径,不停下一步,点击安装. 一路next,最后到了这个页面直接点击finis ...
- 前端在本地启动服务预览html页面
在开发移动端项目时浏览器里出来的效果往往到真机上和预想的有出入,在开发过程中知道了一个可以在本地自己启动一个服务器在手机预览的办法. 1.首先在终端安装http. npm i http-server ...
- Python开发第三方必备工具
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style=&quo ...
- 深入了解组件- -- 动态组件 & 异步组件
gitHub地址:https://github.com/huangpna/vue_learn/example里面的lesson11 一 在动态组件上使用keep-alive 在这之前我们已经有学习过用 ...
- c语言学习笔记 - 顺序查找和哨兵查找比较
今天学习C时用到了顺序查找和哨兵查找,做了一个比较,主要是学习下哨兵查找法 例如在一个数组里查找一个元素,没找到返回-1,找到了则返回这个数组的下标也就是键值. 用循序查找法: void arr_se ...
- 转:linux进程间通信的几种机制的比较及适用场合
源地址:http://blog.csdn.net/f_x_p0324/article/details/6878081 socket 1. # 管道( pipe ):管道是一种半双工的通信方式,数据只能 ...
- Java通过接口或者抽象类调用方法的时候,怎么知道调用的是哪个实现类里的方法?
用对象的 getClass() 方法获得它的类,之后就可以随意去判断这是哪个实现类了. 比如代码1-1所示的JDBC连接池的代码,我们想要知道conn调用的close方法是释放连接还是归还连接, 我们 ...
- Web充斥着存在漏洞的过期JavaScript库
虽然使用第三方软件库通常会降低开发的时间,但同时也会增加网站暴露出的攻击表面,对此我们应有充分的认识.因此需要保持第三方软件库的最新版本依赖,以便从安全更新中获益.即便如此,一份近期研究表明,在Ale ...