题目链接: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. NHibernate 数据查询之Linq to NHibernate

    刚学NHibernate的时候觉得,HQL挺好用的,但是终归没有与其他技术相关联,只有NHibernate用到,一来容易忘记,二来没有智能提示,排除错误什么的都不给力,直到看到一个同事用Linq to ...

  2. Spring @Scheduled定时任务动态修改cron参数

    在定时任务类上增加@EnableScheduling注解,并实现SchedulingConfigurer接口.(注意低版本无效) 设置一个静态变量cron,用于存放任务执行周期参数. 另辟一线程,用于 ...

  3. sqlite3常用指令

    一.建立数据库 sqlite3.exe test.db 二.双击sqlite-3_6_16目录下的程序sqlite3.exe,即可运行 三.退出 .exit 或者 .quit 四.SQLite支持如下 ...

  4. rabbitmq queue_declare arguments参数注释

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_26656329/article/details/77891793说明官方文档 在创建queue ...

  5. var_dump出现省略号的问题

    xdebug.var_display_max_children=128xdebug.var_display_max_data=512xdebug.var_display_max_depth=5

  6. Material Design系列第四篇——Defining Shadows and Clipping Views

    Defining Shadows and Clipping Views This lesson teaches you to Assign Elevation to Your Views Custom ...

  7. 分布式实时日志系统(二) 环境搭建之 flume 集群搭建/flume ng资料

    最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...

  8. React组件设计(转)

    React组件设计 组件分类 展示组件和容器组件 展示组件 容器组件 关注事物的展示 关注事物如何工作 可能包含展示和容器组件,并且一般会有DOM标签和css样式 可能包含展示和容器组件,并且不会有D ...

  9. 移动端rem自适应布局(切图)

    本篇适用于初次使用rem为单位切图而无从下手的童鞋.核心是根据屏幕动态改变根元素字体大小,以达到适配各种屏幕.这只是一个拿来就用的教程.很多东西没有详细说明.不过对于快速做手机端切图很有帮助. 模板: ...

  10. C语言递归练习

    1.炮弹一样的球状物体,能够堆积成一个金字塔,在顶端有一个炮弹,它坐落在一个4个炮弹组成的层面上,而这4个炮弹又坐落在一个9个炮弹组成的层面上,以此类推.写一个递归函数CannonBall,这个函数把 ...