博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The Accomodation of Students HDU - 2444(染色法判断是否为二分图&&二分图匹配)
阅读量:4135 次
发布时间:2019-05-25

本文共 1951 字,大约阅读时间需要 6 分钟。

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对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识。如果可以分成两部分,就算出房间最多需要多少间,否则就输出No。

能否分成两部分,就是这个图能否看成是一个二分图。通常我们用染色法判断这张图是否为一个二分图。取没有标记过的点dfs,如果当前的点染色了,但是它与它父节点被染的颜色相同,说明就不是一个二分图,直接输出No即可。
是二分图之后,就直接匈牙利算法求最大匹配就行。
代码如下:

#include
using namespace std;typedef long long LL;const int N=1111;bool book[N];int match[N],a[N];vector
close[N];bool f(int x){
int m=close[x].size(); for(int i=0 ; i < m ; i++) {
int g=close[x][i]; if(book[g]==false) {
book[g]=true; if(match[g]==-1||f(match[g])) {
match[g]=x; return true; } } } return false;}//用染色法判断是否为二分图,若相邻点同色则不是二分图bool dfs(int x,int c){
a[x]=c; int m=close[x].size(); for(int i=0; i

努力加油a啊,(o)/~

转载地址:http://outvi.baihongyu.com/

你可能感兴趣的文章