The Accomodation of Students

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
题意:判断给出的图是否是二分图,如果是,则求出最大匹配。
 
解法:判断是否是二分图可以用染色法,标记相邻的点为不同颜色,如果标记途中发现要标记的两个点是相同颜色的,则说明这个图不是二分图。如果没遇到这种情况,就说明是二分图。
还有一种判断方法,就是用并查集,具体可以参考:http://www.cnblogs.com/scaugsh/p/5533202.html
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
using namespace std;
int n,m;
int f[];
int A[],B[],match[],book[];
vector <int> V[];//邻接表储存边的关系
void init()
{
memset(match,,sizeof(match));
for (int i=;i<=n;i++)
V[i].clear();
for (int i=; i<=*n; i++)
f[i]=i;
}
int find(int x)
{
int r=x,i=x,t;
while (r!=f[r]) r=f[r];
while (i!=r)
{
t=f[i];
f[i]=r;
i=t;
}
return r;
}
void mix(int x,int y)
{
int fx=find(x),fy=find(y);
f[fx]=fy;
}
bool IsTwo()//染色法求二分图
{
memset(book,,sizeof(book));
queue <int> Q;
Q.push();
book[]=;
while (!Q.empty())
{
int temp=Q.front();
Q.pop();
for (int i=;i<V[temp].size();i++)
{
int num=V[temp][i];
if (book[num]==)
{
book[num]=-book[temp];
Q.push(num);
}
else if (book[num]==book[temp]) return ;
}
}
return ;
}
int dfs(int u)
{
for (int i=; i<V[u].size(); i++)
{
int pos=V[u][i];
if (book[pos]==)
{
book[pos]=;
if (match[pos]==||dfs(match[pos]))
{
match[pos]=u;
return ;
}
}
}
return ;
}
int solve()
{
int ans=;
for (int i=; i<=n; i++)
{
memset(book,,sizeof(book));
if (dfs(i)) ans++;
}
return ans;
}
int main()
{
int a,b;
while (scanf("%d%d",&n,&m)>)
{
init();
int ok=;
while (m--)
{
scanf("%d%d",&a,&b);
if (find(a)==find(b))
ok=;
if (ok)
{
mix(a,b+n);
mix(b,a+n);
V[a].push_back(b);
V[b].push_back(a);
}
}
if (!ok)
{
puts("No");
continue;
}
/*if (!IsTwo())
{
puts("No");
continue;
}*/
printf("%d\n",solve()/);
}
return ;
}
 

The Accomodation of Students(判断二分图以及求二分图最大匹配)的更多相关文章

  1. (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...

  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(判断二分图+最大匹配)

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

  4. hdu 2444 The Accomodation of Students (判断二分图,最大匹配)

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

  5. HDU 2444 The Accomodation of Students(推断是否是二分图)

    题目链接 题意:n个学生,m对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多须要多少个房间. 能否分成两组?也就是说推断是不是二分图,推断二分图的办法,用 ...

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

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

  7. The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)

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

  8. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  9. HD2444The Accomodation of Students(并查集判断二分图+匹配)

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

随机推荐

  1. 【Time系列一】datetime的妙用

    今天在弄个自动关机小脚本的时候,遇到了时间转换的问题,也难怪,以前没学过, 不能怪我不会哦! 首先,先学会打印出当前时间的几种方式 参考开源社区:  http://my.oschina.net/u/1 ...

  2. Linux_jdk

    先查看下 yum list java* yum install java-1.7.0-openjdk* -y 环境变量应该是会自动配置的 或者手动配置编辑/etc/profile #vi /etc/p ...

  3. Linux监控工具vmstat命令详解

        一.前言 很显然从名字中我们就可以知道vmstat是一个查看虚拟内存(Virtual Memory)使用状况的工具,但是怎样通过vmstat来发现系统中的瓶颈呢?在回答这个问题前,还是让我们回 ...

  4. qemu毒液漏洞分析(2015.9)

    0x00背景 安全娱乐圈媒体Freebuf对该漏洞的有关报道: 提供的POC没有触发崩溃,在MJ0011的博客给出了修改后可以使qemu崩溃的poc.详见: http://blogs.360.cn/b ...

  5. 二十八、oracle 视图

    一.介绍视图是一张虚拟表,其内容由查询定义,同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用视 ...

  6. sql语句的学习(2)

    7.统计:学号.姓名.语文.数学.英语.总分.平均成绩 8.列出各门课程的平均成绩.课程,平均成绩 9.列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名) 10.列出数学成绩在2-3名的学生( ...

  7. Intent的几种Flag的不同

    冬天有点冷,不想写博客. 研究下Intent的几种Flag的不同: 1,FLAG_ACTIVITY_CLEAR_TOP:会清理掉目标activity栈上面所有的activity Intent inte ...

  8. SSM

    今天内容安排 1:复习mybatis 2:复习springMVC 3:springMVC+spring+mybatis组合起来,搭建一个web应用开发的框架 4:用户管理系统,针对用户的CRUD操作, ...

  9. 利用Fiddler抓取websocket包

    一.利用fiddler抓取websockt包 打开Fiddler,点开菜单栏的Rules,选择Customize Rules... 这时会打开CustomRules.js文件,在class Handl ...

  10. 一键批量ping任意ip段的存活主机

    =======================by me===================================== @echo offecho.color FC for /f %%i ...