题目链接:https://vjudge.net/problem/HDU-2444

The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7328    Accepted Submission(s): 3270

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
 
Source
 
Recommend
gaojie

题解:

任务1:能否把这些人分成两组,且在每一组内,所有人互不相识(即两点间没有边直接相连)?

任务2:求最大匹配数, 直接用hungary()算法。

1.由于要把所有点分成两组,所以我们可以用两种颜色,对整幅图进行染色。规定:相邻两点间的颜色不同,然后把颜色相同的归为一组。

2.对于没有被染色的点u,对其进行染色,然后遍历所有与之相连的点v,如果点v没有被染色,则对其进行访问,染上另外一种颜色;如果点v已经被染色,则根据点u和点v的的染色情况来判断是否有冲突:

3.如果颜色相同,即把他们放在同一组,但他们是相互认识的,不能放在同一组,所以产生了冲突;如果颜色不同,则他们被分在了两组,符合要求。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int n;
char a[MAXN][MAXN];
int M[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = ; i<=n; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=n; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} int col[MAXN];
bool Color(int u, int c) //染色,如果有冲突,则返回true
{
col[u] = c;
for(int i = ; i<=n; i++)
if(M[u][i])
{
if(col[i]==col[u]) return true; //与之前访问过的点相连,且为同色,则有冲突。
if(col[i]==- && Color(i, !c)) return true; //如果没有没有访问过,则对其染色。
}
return false;
} int main()
{
int m;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(M, false, sizeof(M));
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
M[u][v] = M[v][u] = true;
} bool flag = false;
memset(col, -, sizeof(col));
for(int i = ; i<=n; i++) //染色
if(col[i]==-)
flag = flag|Color(i, ); if(flag)
{
printf("No\n");
continue;
} int cnt = hungary();
printf("%d\n", cnt/);
}
}

HDU2444 The Accomodation of Students —— 二分图最大匹配的更多相关文章

  1. HDU2444 The Accomodation of Students(二分图最大匹配)

    有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...

  2. hdu_2444The Accomodation of Students(二分图的判定和计算)

    hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...

  3. HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)

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

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

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

  5. HDU2444 The Accomodation of Students

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

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

  7. hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)

    //推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...

  8. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  9. HDU2444 The Accomodation of Students【匈牙利算法】

    题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...

随机推荐

  1. Run-time Settings--General--Run Logic

    LR单用户,重复操作日志 案例:假如你想在一个脚本中,实现登录执行1次,查询执行2次,插入执行3次,怎么办?录3个脚本?每个事务分别在脚本中复制N次? 当然不用,LR早就想到了你的需求,下面让我们隆重 ...

  2. Laya Tween 和 遮罩

    Laya Tween 和 遮罩 @author ixenos 场景:在使用Tween循环时,不规则物体部分超出范围 方案:使用遮罩定型 困境:在laya ide设计模式中将遮罩sprite放到不规则物 ...

  3. PatentTips - Wear Leveling for Erasable Memories

    BACKGROUND Erasable memories may have erasable elements that can become unreliable after a predeterm ...

  4. Linux MTD (Memory Technology Device) subsystem analysis -For Atheros char device

    Linux MTD (Memory Technology Device) subsystem analysis For Atheros char device 读了Linux MTD 源代码分析 对这 ...

  5. DatePickerDialog

    package com.pingyijinren.helloworld.activity; import android.app.DatePickerDialog; import android.su ...

  6. poj1523求割点以及割后连通分量数tarjan算法应用

    无向图,双向通道即可,tarjan算法简单应用.点u是割点,条件1:u是dfs树根,则u至少有2个孩子结点.||条件2:u不是根,dfn[u]=<low[v],v是u的孩子结点,而且每个这样的v ...

  7. Spring MVC中 log4j日志文件配置相对路径

    log4j和web.xml配置webAppRootKey 的问题 1 在web.xml配置 <context-param>  <param-name>webAppRootKey ...

  8. 使用datatables实现后台分页功能,减轻前端渲染压力

    注意不同版本,参数名字及参数内容存在差异,具体可以参考https://datatables.net/upgrade/1.10-convert#Options 控制页面显示的参数:https://dat ...

  9. db2安装配置备份还原

    环境 cenos 7.0 db2版本 db2_v101_linuxx64_expc.tar 安装db2 解压db2 tar zxvf db2_v101_linuxx64_expc.tar cd exp ...

  10. 新浪微博发送消息和授权机制原理(WeiboSDK)

    1.首先是在微博发送消息,对于刚開始做weibo发送消息的刚開始学习的人会有一个误区,那就是会觉得须要授权后才干够发送消息.事实上发送消息仅仅须要几行代码就能够实现了,很easy,不须要先授权再发送消 ...