传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444

题意:首先判断所有的人可不可以分成互不认识的两部分。如果可以分成 ,则求两部分最多相互认识的对数。

思路:二分图最大匹配问题。先BFS判断是否为二分图,再用匈牙利算法算最大匹配量

关于匈牙利算法:https://blog.csdn.net/CillyB/article/details/55511666

代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
int map[205][205];
int vis[205];
int girl[205];
int n, m;
int find(int x)
{
for(int y = 1; y <= n; y++)
{
if(map[x][y] && vis[y] == 0)
{
vis[y] = 1;
if(girl[y] == 0 || find(girl[y]))
{
girl[y] = x;
return 1;
}
}
}
return 0;
}
bool solve()
{
queue<int>q;
q.push(1);
vis[1] = 1;
while(!q.empty())
{
int x = q.front();
q.pop();
for(int i = 1; i <= n; i++)
{
if(map[x][i])
{
if(vis[i] == 0)
{
if(vis[x] == 1)
vis[i] = 2;
else
vis[i] = 1;
q.push(i);
}
else if(vis[i] == vis[x])
return false;
}
}
}
return true;
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
map[i][j] = map[j][i] = 0;
vis[i] = 0;
}
for(int i = 1; i <= m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
map[a][b] = map[b][a] = 1;
}
if(n == 1 || !solve())
{
cout << "No" << endl;
continue;
}
else
{
int ans = 0;
memset(girl,0,sizeof(girl));
for(int i = 1; i <= n; i++)
{
memset(vis, 0, sizeof(vis));
ans += find(i);
}
cout << ans / 2 << endl;
}
}
}

 

HDU 2444 The Accomodation of Students【二分图最大匹配问题】的更多相关文章

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

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

  2. 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 ...

  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二分图判定和匈牙利算法

    本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...

  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(判断二分图+最大匹配)

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

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

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

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

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

  9. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

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

随机推荐

  1. duilib 之 List控件

    List控件是我们常用到的控件,也是应用很广泛. 对LIST控件添加元素有两种方法,一种是直接在XML中写死元素,另一种是动态创建.另外,LIST的应用也分为两种,一种需要表头,另一种是不需要表头.对 ...

  2. asp.net mvc3用file上传文件大小限制问题

    在Windows2008下,如果上传比较大的文件,可能会出现404错误,(请求筛选模块被配置为拒绝超过请求内容长度的请求). 可通过如下方法解决: 打开URTracker根目录下的web.config ...

  3. 082-PHP的do-while循环break跳出

    <?php $i = 1; do { echo $i; $i = $i + 1; if ($i >= 5) { echo "break<br>"; brea ...

  4. Swift 类的使用class

    /* 类属性的介绍 Swift中类的属性有多种 1.存储属性:存储示例的常量和变量 2.计算属性:通过某种方式计算出来的属性 3.类属性:与整个类自身相关的属性 存储属性 存储属性是最简单的属性,它作 ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-flag

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. Arduino - Nano针脚分配时需要注意的事项

    0.1为Rx.Tx 针脚,这两个针脚一般作为串口使用,非串口设备尽量不占用该针脚.2.3为中断口,分别对应中断0.中断1,需要中断功能的设备,必须接入此.2-13.A0-A5,共18个针脚,都可以作为 ...

  7. java课程课后作业190530之用户体验评价

    每个人评价一下大家手头正在使用输入法或者搜索类的软件产品. 从用户界面.记住用户选择.短期刺激.长期使用的好处坏处.不要让用户犯简单的错误四个方面发表一篇博客. 输入法:苹果自带的输入法 用户界面:简 ...

  8. ABP框架没有httpPost,httpget,httpput特性

    需要引用一下组件, Microsoft.AspNetCore.Mvc

  9. C语言-浮点类型

    C语言-浮点类型 浮点类型 在0的两侧有一小块区域,这个区域非常接近0,但是不等于0,是float(表达范围数量级10^-38^)或者double(达范围数量级10^-308^)无法表达的,而0是可以 ...

  10. python实现微信发送服务器监控报警消息代码实现

    这篇文章主要介绍了python3.8 微信发送服务器监控报警消息代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 ! python版本 > ...