(hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444
Problem Description
There 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. Input
For each data set:
The first line gives two integers, n and m(<n<=), 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. Output
If 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 Input Sample Output
No
题意:有n个学生,a与b认识,b与c认识,但a与c不认识,问把学生分到两个房间,使每个房间的人都不认识,一个房间最多多少人?
方法:先判断是否能成二分图 不能输出No,能求最大匹配数
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define N 609
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
int Map[N][N],vis[N],used[N],cl[N];
int n;
int dfs()
{
int cot;
met(cl,-);
queue<int> Q;
Q.push();
cl[]=;
while(Q.size())
{
cot=Q.front();
Q.pop();
for(int i=;i<=n;i++)
{
if(Map[cot][i])
{
if(cl[i]==-)
{
cl[i]=-cl[cot];
Q.push(i); }
else if(cl[i]==cl[cot])
return ;
}
}
}
return ;
}
int han(int u)
{
for(int i=;i<=n;i++)
{
if(!vis[i] && Map[u][i])
{
vis[i]=;
if(!used[i] || han(used[i]))
{
used[i]=u;
return ;
}
}
}
return ;
}
int main()
{
int t,m,a,b;
while(scanf("%d %d",&n,&m)!=EOF)
{
met(Map,); for(int i=;i<m;i++)
{
scanf("%d %d",&a,&b);
Map[a][b]=Map[b][a]=;
}
if(dfs()==)///判断是否能成二分图
{
printf("No\n");
continue;
}
else
{
int sum=;met(used,);
for(int i=;i<=n;i++)
{
met(vis,);
sum+=han(i);
} printf("%d\n",sum/);
}
}
return ;
}
(hdu)2444 The Accomodation of Students 判断二分图+最大匹配数的更多相关文章
- 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 ( ...
- hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 2444 The Accomodation of Students【二分图最大匹配问题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
[题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...
- hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配
此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
- 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 ...
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
随机推荐
- DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践
http://www.cnblogs.com/xishuai/p/ddd-repository-iunitofwork-and-idbcontext.html
- AQTime教程(1)
AQTime教程 1 简介 AQTime和MemProof都是AutomatedQA旗下的产品,AQTime比MemProof提供了更丰富强大的功能.该产品含有完整的性能和调试工具集,能够收集程序运行 ...
- 解决MySQL连接超时Communications link failure due to underlying exception
最近在用一个MySQL的Java连接池的过程中,连接一晚上不释放,第二天就会造成超时的错误,查了一下原因,原来是因为MySQL默认的空闲等待时间是8个小时,一旦空闲超过8个小时,就会抛出异常.异常文本 ...
- cocos2d-x中本地推送消息
作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4038277.html IOS下很简单: 添加一条推送 void PushNotific ...
- UIView中触摸事件touchBegin
UIView触摸事件touchBegin 等一系列方法 1)手指按下 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; ...
- 修改hosts文件(判断是否为管理员/以管理员权限运行脚本)
将以下命令保存为 HostsModify.ps1,然后执行即可 #该脚本用来添加hosts解析记录.脚本在执行的时候会判断当前用户是否为管理员,如果不是则弹出提示框口,要求输入相应密码 If (-NO ...
- 安装Windows SDK7.1时发生的一个错误(附解决办法)
A problem occurred while installing selected Windows SDK components. Installation of the "Micro ...
- struts2标签具体解释
要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部增加下面的代码: <%@taglib prefix="s" uri="/struts ...
- iOS之宏定义#define
最基本的宏定义用法 #define aaa bbb 表示用aaa替换bbb的内容. 宏作用范围 宏的作用范围是在当前文件内, 如果需要作用于其他类(如在类b调用类a已定义宏),那么需要在类b引入类a的 ...
- 【jQuery插件】用jQuery Masonry快速构建一个pinterest网站布局(转)
[jQuery插件]用jQuery Masonry快速构建一个pinterest网站布局 时间:2011年03月21日作者:愚人码头查看次数:29,744 views评论次数:25条评论 前段时间领导 ...