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. adb shell dumpsys

    adb shell dumpsys activity activities -- class/packagename adb shell dumpsys batterystate --reset   ...

  2. freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建

    今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...

  3. haoce修改mysql

    修改时长余额 select * from sys_user_product up where up.user_id in(select u.id from sys_user u where login ...

  4. MD5的Hash长度扩展攻击

    Hash长度扩展攻击 引子 无意中碰到一道题,大概代码是这样的 $flag = "XXXXXXXXXXXXXXXXXXXXXXX"; $secret = "XXXXXXX ...

  5. hibernate主键generator属性介绍

    increment(递增) 用于为long, short或者int类型生成唯一标识.只有在没有其他进程往同一张表中插入数据时才能使用. 在集群下不要使用. identity (标识)对DB2,MySQ ...

  6. VMware安装Centos6.8设置ip无法远程连接问题

    今天使用VMware安装Centos6.8minimal版本再设置ip地址的时候遇到了一些麻烦,就是无法ping通Centos操作系统的配置的ip从而无法用Xshell远程连接上. 如何配置请看下面的 ...

  7. hdu1024

    #include <cstdio>#include <iostream>const int MAX = 1000005; using namespace std; int nu ...

  8. hdu1002

    //c//https://github.com/ssdutyuyang199401/hduacm/blob/master/1002.c#include<stdio.h>#include&l ...

  9. Snacks

    Snacks 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5692 dfs序+线段树 这道题涉及到对整棵树的值修改,考虑将树状结构用dfs ...

  10. ECMAScript6标准新增加的内容

    首选呐,你得了解一下javascript和ECMAScript的关系: 编程语言JavaScript是ECMAScript的实现和扩展,由ECMA(一个类似W3C的标准组织)参与进行标准化.ECMAS ...