题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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

 
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
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
 
Sample Output
No
3
 
题意:
给出N个点,M条边,判断是否是二分图,如果是则输出最大匹配数;
 
题解:
DFS或者BFS判断是否为二分图;
匈牙利算法求最大匹配数;
 
AC代码:
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define MAX 203
using namespace std;
//匈牙利算法 - st
int n,m;
struct Edge{
int u,v;
};
vector<Edge> E;
vector<int> G[MAX];
int matching[MAX];
int vis[MAX];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
void add_edge(int u,int v)
{
E.push_back((Edge){u,v});
E.push_back((Edge){v,u});
int _size=E.size();
G[u].push_back(E.size()-);
G[v].push_back(E.size()-);
}
bool dfs(int u)
{
for(int i=,_size=G[u].size();i<_size;i++)
{
int v=E[G[u][i]].v;
if (!vis[v])
{
vis[v]=;
if(!matching[v] || dfs(matching[v]))
{
matching[v]=u;
matching[u]=v;
return true;
}
}
}
return false;
}
int hungarian()
{
int ret=;
memset(matching,,sizeof(matching));
for(int i=;i<=n;i++)
{
if(!matching[i])
{
memset(vis,,sizeof(vis));
if(dfs(i)) ret++;
}
}
return ret;
}
//匈牙利算法 - ed
bool judge()
{
memset(vis,,sizeof(vis));
queue<int> q;
q.push();
vis[]=;
while(!q.empty())
{
int now=q.front();q.pop();
for(int i=,_size=G[now].size();i<_size;i++)
{
int nex=E[G[now][i]].v;
if(!vis[nex])
{
q.push(nex);
vis[nex]=-vis[now];
}
if(vis[nex]==vis[now]) return false;//不是二分图
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init(,n);
for(int i=,u,v;i<=m;i++)
{
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
if(!judge()) printf("No\n");
else printf("%d\n",hungarian());
}
}

PS.这里因为懒得枚举U集里的点(因为这要先得到U集里的点都是哪些),所以直接枚举所有点;

HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]的更多相关文章

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

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

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

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

  3. HDU 2444 The Accomodation of Students二分图判定和匈牙利算法

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

  4. HDU 2444 The Accomodation of Students(判断是否可图 + 二分图)

    题目大意:有一群人他们有一些关系,比如A认识B, B认识C, 但是这并不意味值A和C认识.现在给你所有互相认识的学生,你的任务是把所有的学生分成两个一组, 住在一个双人房里.相互认识的同学可以住在一个 ...

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  7. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  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. 微信企业号OAuth2验证接口实例(使用SpringMVC)

    微信企业号OAuth2验证接口(使用SpringMVC) 企业应用中的URL链接(包含自己定义菜单或者消息中的链接).能够通过OAuth2.0来获取员工的身份信息. 注意.此URL的域名,必须全然匹配 ...

  2. VMWare------安装时出现无法将值写入注册表项

    安装时提示详情: 无法打开注册表项UNKNOWN\Components\...请确认你是否有足够的权限访问该注册表项,或者与技术支持人员联系. 解决方法: 关掉360安全卫士等软件再安装

  3. Docker-创建和分享应用(3)

          Dockerfile定义容器内环境中发生的事情.对网络接口和磁盘驱动器等资源的访问在此环境中进行虚拟化,该环境与系统的其他部分隔离,因此您需要将端口映射到外部世界,并具体说明要“复制”哪些 ...

  4. 我的WAF Bypass实战系列

    ​ 梳理了一下自己写过的WAF Bypass相关的文章,按照编写时间顺序,整理成了一个WAF Bypass实战系列,如果你准备了解WAF攻防这一块的内容,可以来了解一下. 第一篇:<Bypass ...

  5. FFMPEG转换WAV到MP3

    下载FFMPEG https://ffmpeg.zeranoe.com/builds/ Example to encode VBR MP3 audio with ffmpeg using the li ...

  6. Unity 蓝牙插件

    1.新建一个Unity5.6.2f1工程,导入正版Bluetooth LE for iOS tvOS and Android.unitypackage2.用JD-GUI反编译工具查看unityandr ...

  7. 这样理解 HTTPS 更容易(Maybe)

    摘要:本文尝试一步步还原HTTPS的设计过程,以理解为什么HTTPS最终会是这副模样.但是这并不代表HTTPS的真实设计过程.在阅读本文时,你可以尝试放下已有的对HTTPS的理解,这样更利于“还原”过 ...

  8. 正则-input控制输入

    大于0的数字:/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/  这正则看不太懂,先放着 作者:Kevin Yang 使用正则表达式找出不包含特定字符串 ...

  9. WP8.1学习系列(第四章)——交互UX之导航模式

    交互模式和指南 这部分包括三部分内容,分别是导航模式.命令模式和输入模式. 导航模式 虽然 Windows 导航模式提供了框架,但它提倡创新.激发你的创造力并在已建立的模式上构建. 命令模式 使用应用 ...

  10. 【python3】window下 vscode 配置 python3开发环境

    本文以python3.7 为例 一 下载python3 url : https://www.python.org/downloads/windows/ 提示: 安装过程中.记得勾选  添加环境变量 二 ...