传送门: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. usb摄像头驱动的移植

    相关软件下载地址:http://pan.baidu.com/s/16yo8Y 1.使用摄像头型号ov9650 ①修改.配置内核 1.修改vi drivers/i2c/busses/Kconfig (参 ...

  2. cf 498 B. Name That Tune

    不会不会,,,%%http://hzwer.com/5813.html #include<cstdio> #include<iostream> #include<algo ...

  3. 【问题管理】-- Tomcat8部署项目加载静态资源html页面编码错误

    1.问题背景及解决方式 最近在回顾Tomcat部署Web项目,自己简单地从Tomcat的下载安装及配置server.xml文件入手,学习Tomcat的项目部署,在自己使用IDEA创建了一个简单地web ...

  4. Python 中 对logging 模块进行封装,记录bug日志、日志等级

    是程序产生的日志 程序员自定义设置的 收集器和渠道级别那个高就以那个级别输出 日志和报告的作用: 报告的重点在于执行结果(执行成功失败,多少用例覆盖),返回结果 日志的重点在执行过程当中,异常点,哪里 ...

  5. .net Form 的Autoscalemode属性

    .net Form 的Autoscalemode属性如果设置成Font 将会随着系统字体的大小来改变form大小 有时候会造成布局混乱,小心使用

  6. vue学习(四)插槽

    一 匿名插槽 // 语法 Vue.component('MBtn', { template: ` <button> <slot></slot> </butto ...

  7. POJ 1423:Big Number 求N的阶乘的长度 斯特林公式

    Big Number Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27027   Accepted: 8626 Descr ...

  8. HDU 5280 BestCoder Round #47 1001:Senior's Array

    Senior's Array  Accepts: 199  Submissions: 944  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit ...

  9. 使用node.js安装asar和反编译app.asar

    背景:app.asar文件是Electron加密打包时的中间产物,electron.exe调用resources文件夹下的app.asar从而实现不用解压缩而直接读取文件内容的高效. 一.需要先安装n ...

  10. linux搭建mariadb,windows2008搭建iis+php+wordpress

    centos ip:192.168.101 windows ip :192.168.102 centos配置: [root@ml ~]# yum -y install mariadb-server # ...