The Accomodation of Students

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

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
 
题意:给定n个点m条边的无向图,判断是否为二分图,如果不是输出No,是则输出最大匹配。
判断是否为二分图:dfs,如果某个点u没有赋id,则赋为1,找到所有与他相邻的点,如果存在某个点id和该点相同则return false; 如果id==0则赋为-1*id[u]继续dfs;
求最大匹配用匈牙利算法,注意由于原图是个无向图,是自己人工把他当成二分图,所以如果左边的点1和右边的点2之间有边,则右边的点1和左边的点2之间也有边,所有求出的最大匹配要除以2;
/*
ID: LinKArftc
PROG: 2444.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int maxm = ; int mp[maxn][maxn];
int linker[maxn];
int id[maxn];
int uN, vN;
int n, m;
bool vis[maxn]; bool dfs(int x) {
for (int i = ; i <= n; i ++) {
if (mp[x][i]) {
if (id[i] == id[x]) return false;
if (id[i] == ) {
id[i] = - * id[x];
if (!dfs(i)) return false;
}
}
}
return true;
} bool dfs1(int u) {
for (int v = ; v <= n; v ++) {
if (mp[u][v] && !vis[v]) {
vis[v] = true;
if (linker[v] == - || dfs1(linker[v])) {//刚开始这地方写成dfs了,罪过呀,写的有点混乱,下次注意!
linker[v] = u;
return true;
}
}
}
return false;
} int hungry() {
memset(linker, -, sizeof(linker));
int ret = ;
for (int i = ; i <= n; i ++) {
memset(vis, , sizeof(vis));
if (dfs1(i)) ret ++;
}
return ret;
} int main() {
//input;
int u, v;
while (~scanf("%d %d", &n, &m)) {
memset(mp, , sizeof(mp));
memset(id, , sizeof(id));
for (int i = ; i <= m; i ++) {
scanf("%d %d", &u, &v);
mp[u][v] = ;
mp[v][u] = ;
}
bool flag = true;
for (int i = ; i <= n; i ++) {
if (!id[i]) {
id[i] = ;
if (!dfs(i)) {
flag = false;
break;
}
}
}
if (!flag) {
printf("No\n");
continue;
} else printf("%d\n", hungry() / );
}
return ;
}

HDU2444(判断是否为二分图,求最大匹配)的更多相关文章

  1. UVALive 2523 Machine Schedule(二分图求最大匹配数)

    题意:有两台机器,上面有多个工作区域,有多个任务,分别可以在两台机器的某一个区域上完成,两台机器一开始都在0区域上工作,每次更改区域,都会重新启动一次,让我们求出最小的重启次数. 思路:将两个区域连线 ...

  2. hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配

    此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...

  3. 染色法判断是否是二分图 hdu2444

    用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 ...

  4. uva12083 二分图 求最大独立集 转化为求最大匹配 由题意推出二分图

    这题大白书例题 : Frank 是一个思想有些保守的高中老师,有一次,他需要带一些学生出去旅行,但又怕其中一些学生在旅途中萌生爱意.为了降低这种事情的发生概率,他决定确保带出去的任意两个学生至少要满足 ...

  5. hdu2444The Accomodation of Students (最大匹配+判断是否为二分图)

    题意 首先判断所有的人可不可以分成两部分,每部分内的所有人都相互不认识.如果可以分成 则求两部分最多相互认识的对数. 解题 类似分成两组,同组互不相关,就可能使判断是否为二分图 能否分成两部分 则是判 ...

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

    这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

  7. HDU 2389 Rain on your Parade / HUST 1164 4 Rain on your Parade(二分图的最大匹配)

    HDU 2389 Rain on your Parade / HUST 1164 4 Rain on your Parade(二分图的最大匹配) Description You're giving a ...

  8. The Accomodation of Students---hdu2444(二分图,最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:有n个学生,m个关系,但是如果a认识b,b认识c,但是a不一定认识c: 求能不能把这n个人 ...

  9. hdu3729 I'm Telling the Truth (二分图的最大匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. POJ 1149 PIGS(最大流)

    Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...

  2. 【iOS开发】iOS CGRectGetMaxX/Y 使用

    在iOS的界面布局中我们可以使用CGRectGetMaxX 这个方法来方便的获取当前控件的x坐标值+宽度的数值,这样便可以方便布局. 同理CGRectGetMaxY是获取y坐标值+控件高度的值,当然这 ...

  3. chromium源码阅读--V8 Embbeding

    V8是google提供高性能JavaScript解释器,嵌入在chromium里执行JavaScript代码. V8本身是C++实现的,所有嵌入本身毫无压力,一起编译即可,不过作为一个动态语言解释器, ...

  4. 关于<!DOCTYPE html>的学习(转)

    DOCTYPE是对Document type的缩写,说明用XHTML或者HTML是什么版本的.必须出现在<html>标签的前面,不需要关闭标签. <!DOCTYPE>声明不是标 ...

  5. PAT 1065 单身狗

    https://pintia.cn/problem-sets/994805260223102976/problems/994805266942377984 “单身狗”是中文对于单身人士的一种爱称.本题 ...

  6. JVM(2)——GC算法和收集器

    一.引入 上篇博客<JVM--简介>中主要介绍了JVM的内存模型,思考一下: 为什么要划分堆.栈.方法区等? 为什么把不同种类的数据信息分别存放? 答案可以分为很多很多条,这里就说一个方面 ...

  7. 【SSH】——使用ModelDriven的利与弊

    在以往的web开发中,如果要在表单显示什么内容,我们就需要在Action中提前定义好表单显示的所有属性,以及一系列的get和set方法.如果实体类的属性非常多,那么Action中也要定义相同的属性.在 ...

  8. Sql Server性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON

    1.前言 对于优化SQL语句或存储过程,以前主要是用如下语句来判断具体执行时间,但是SQL环境是复杂多变的,下面语句并不能精准判断性能是否提高:如果需要精确知道CPU.IO等信息,就无能为力了. ), ...

  9. CSS的基本使用

    CSS的出现就是为了将HTML的内容与样式分离 CSS的书写方式 selector{ key:value } h1{ color: blue; } <!DOCTYPE html> < ...

  10. IE提示是否只查看安全传送的网页内容

    IE选项-->安全-->点上面那个地球internet-->点下面那个 自定义级别-->找到“其他”-->显示混合内容,改为启用,重启打开下IE,就可以了