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.

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

OutputIf 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

tips:给这些点涂上黑白两色,就可以表示分出集合了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std; const int maxn = 205;
int n, m;
/*struct edge{
int from, to;
edge(int f, int t): from(f), to(t){}
};*/ vector <int> G[maxn];
//vector <edge> edges;
int col[maxn],pre[maxn], flag[maxn];
int check[maxn]; bool dfs(int u, int tar)//黑白标色 找到NO的情况 也就是路径上间隔1的点颜色相同
{
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(col[v] == -1){
check[v] = true;
col[v] = tar ^ 1;
if(!dfs(v, col[v])){
return false;
}
}
else if(col[v] == (tar ^ 1)) check[v] = true;
else if(col[v] == tar) return false;
}
return true;
} int ffind(int u)//找增广路
{
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(!flag[v]){
flag[v] = true;
if(pre[v] == -1 || ffind(pre[v])){
pre[v] = u;
return true;
}
}
}
return false;
} void init()
{
for(int i = 0; i < n; i++){
G[i].clear();
}
memset(pre, -1, sizeof(pre));
memset(col, -1, sizeof(col));
memset(check, false, sizeof(check));
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF){
//int k = 0;
init();
for(int i = 0; i < m; i++){
int f,t;
scanf("%d%d",&f,&t);
G[f - 1].push_back(t - 1);
//edge e(f - 1,t - 1);
//edges.push_back(e);
}
bool tar = false;
for(int i = 0; i < n; i++){
if(check[i]) continue;
col[i] = 1;
if(!dfs(i, col[i])){
tar = true;
break;
}
}
if(tar){
printf("No\n");
continue;
}
int sum = 0;
for(int i = 0; i < n; i++){
memset(flag, false, sizeof(flag));
sum += ffind(i);
}
printf("%d\n",sum);
}
return 0;
}

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(二分匹配 匈牙利算法 邻接表实现)

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

  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 (二分图存在的判定以及最大匹配数)

    There are a group of students. Some of them may know each other, while others don't. For example, A ...

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

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

  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 2444 The Accomodation of Students 判断二分图+二分匹配

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

  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 StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  10. HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)

    题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...

随机推荐

  1. mysql 用户权限操作

    https://www.cnblogs.com/SQL888/p/5748824.html http://blog.csdn.net/fafa211/article/details/2249217

  2. RESTEasy maven使用

    添加依赖: <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteas ...

  3. ios开发之--使用xib适配iPhone X

    最近在修改一个老项目,里面有很多xib文件,需要适配iPhone X,但是又不想重写页面用代码适配,分享个小方法,也算是个笨办法吧, 适配iPhone X底部,iPhone X底部有34px的操作区域 ...

  4. Lua协程-测试2

    print("Lua 协程测试2") function testFun(n) print("into foo,n = "..n) * n) -- 挂起co协程 ...

  5. CMD打开模拟器

    CMD-> CD d:\android\android-sdk-151\tools-> (这里的路径是你emulator.exe所在的路径) emulator -avd avdname-& ...

  6. React Native(六)——PureComponent VS Component

    先看两段代码: export class ywg extends PureComponent { …… render() { return ( …… ); } } export class ywg e ...

  7. 普通for循环和增强for循环的区别

    1.普通for循环:自行维护循环次数,循环体自行维护获取元素的方法: int[] array = new int[]{1,2,3,4,5}; //int[] array ={1,2,3,4,5} ; ...

  8. iOS - 选取相册中iCloud云上图片和视频的处理

    关于iOS选取相册中iCloud云上图片和视频  推荐看:TZImagePickerController的源码,这个是一个非常靠谱的相册选择图片视频的库 .当然也可以自己写 如下遇到的问题 工作原因, ...

  9. Python学习(23):Python面向对象(1)速成

    转自 http://www.cnblogs.com/BeginMan/p/3190776.html 一.Python经典类与新类 经典类:如果没有直接或间接的子类化一个对象,也就是说如果没有指定一个父 ...

  10. Web编辑器的使用

    1.复制web编辑器到你的项目中的表现层(UI) 2.添加引用:FredCK.FCKeditorV2.dll到你的项目中来 3.页面中加引用 <%@ Register TagPrefix=&qu ...