(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个人认识能够去一个双人 ...
随机推荐
- Attribute特性验证模型model
数据验证我们往往分为前台验证和后台验证,而我们的后台验证每到一个方法中就要去验证一次,这样的代码想想都难以维护,这篇我们这篇文章就是为了解决这样的问题.用attribute 这个特性来解决这样的问题 ...
- 使用教程sqlite
访问地址: http://www.runoob.com/sqlite/sqlite-where-clause.html
- Android开发日志问题
以前在Android开发中发现,日志打印好多,调试的时候各种加Log,之后就各种不删除,导致项目后期花大把时间删除日志打印. 学到一个好方法: 在所有尽可能高的父类里面加上一个常量 DEBUG ,一开 ...
- 教你50招提升ASP.NET性能(六):为了生动的用户体验,总是在客户端验证
(12)For a snappy user experience, always validate on the client 招数12: 为了生动的用户体验,总是在客户端验证 To avoid un ...
- cocos2d-x sprite触摸处理
转自:http://www.cnblogs.com/lancidie/archive/2013/04/01/2993890.html 我们常常需要判断用户的点击操作是否落于某个sprite之上,进而让 ...
- cocos2d-x 纹理研究
转自:http://blog.csdn.net/qq51931373/article/details/9119161 1.通常情况下用PVR格式的文件来进行图片显示的时候,在运行速度和内存消耗方面都要 ...
- materialish-progress
https://github.com/pnikosis/materialish-progress materialish-progress-master.zip
- [Whole Web] [Node.js] Using npm run to launch local scripts
npm run allows you to configure scripts inside of your package.json file which can access locally in ...
- Android TabHost TabWidget 去除黑线(底部下划线)
采用TabHost布局时,往往会发现默认的系统风格与软件风格很不协调,比如TabWidget的下划线影响布局效果.通常情况下会去除其下划线.如果是采用xml布局文件,在TabWidget的属性项设置a ...
- (function(){}).call(window) 严格模式匿名函数的this指向undefined
上次在群里,看到有人发出 (function(){}).call(window) 这么一段代码,问这有什么意义,匿名函数中的this不是始终都指向window的么,为什么还要call,我当时也很疑惑. ...