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. Qt程序加图标

    第一步 准备一个ICON图标 例如:myicon.ico 新建文本文件,里面编辑文字 IDI_ICON1 ICON DISCARDABLE "myicon.ico" 文件另存为 x ...

  2. LeetCode 全解(bug free 训练)

    1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  3. 数据挖掘算法:k-means算法的C++实现

    (期末考试要到了,所以比较粗糙,请各位读者理解..) 一.    概念 k-means是基于原型的.划分的聚类技术.它试图发现用户指定个数(K)的簇(由质心代表).K-means算法接受输入量K,然后 ...

  4. ORM选型对比

    ORM框架选型 ORM框架选型 jian A YEAR AGO (2017-04-10) orm, database 选型标准:实现O/R mapping,基于promise,支持原生SQL语句,支持 ...

  5. Java生成C#可用Model包

    项目需要提供接口给.NET团队使用,为方便大伙,特地写一个从Java接口生成C#可用Model包的工具Class 主Class是一个Controller,可以随时进行生成 package com.fa ...

  6. await和async再学习

    await太不容易理解了,自己常常迷惑,不知道该怎么用. 文章:探索c#之Async.Await剖析 这篇文章,有一个很清晰的描述: 使用Async标记方法Async1为异步方法,用Await标记Ge ...

  7. 搭建springmvc项目没扫描到mapper和service

    严重: Servlet.service() for servlet [spring] in context with path [/springmvc-demo] threw exceptionorg ...

  8. Linux设置快捷命令

    vi ~/.bashrc 在.bashrc目录中,添加 alias 设置 例如 cdtools='cd ~/GIT/tools' 对于一条比较长的命令,如显示系统运行时长 cat /proc/upti ...

  9. SRM710 div1 MagicNim(博弈论)

    题目大意: 给出n+1堆石子,前n堆石子的数量是a[i],最后一堆只有1个石子,但是具有魔力 拿走该石子的一方可以选择接下来是进行普通的Nim游戏还是anti-nim游戏 问是先手必胜还是必败 首先拿 ...

  10. 整理一些JavaScript时间处理扩展函数

    在JavaScript中,时间处理是经常需要用到的.最近想要慢慢建立自己的代码库,整理了几个之前用到的js处理时间的函数,发出来跟大家分享一下,以后的使用中会不断增加和修改代码库. 把字符串转换为日期 ...