http://acm.hdu.edu.cn/showproblem.php?pid=2444
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
 
题目大意: 有n个人,和m种关系,代表a和b认识。要求将n个人放入到两个集合中,每个集合中的人互不认识,A集合中的人找B集合的人住房间,两个人一间,且两个人必须认识,问你最多能住多少间房间。
 
判断是否为二分图:在无向图G中,如果存在奇数回路,则不是二分图。否则是二分图。
判断回路奇偶性:把相邻两点染成黑白两色,如果相邻两点出现颜色相同则存在奇数回路。也就是非二分图。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define N 210 int G[N][N], vis[N], used[N];
int m, n, f, c[N]; bool Find(int u)
{
int i;
for(i = ; i <= n ; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
} void DFS(int x, int color)
{
int i;
for(i = ; i <= n ; i++)
{
if(G[x][i])
{
if(c[i] == )
{
c[i] = -color;//将x相邻的点i染成与其不同的颜色
DFS(i, -color);
}
else if(c[i] == color)//如果相邻两点颜色相同则不是二分图
{
f = ;
return ;
}
}
}
} int main()
{
int i, a, b;
while(~scanf("%d%d", &n, &m))
{
memset(G, , sizeof(G));
memset(c, , sizeof(c));
f = ;
while(m--)
{
scanf("%d%d", &a, &b);
G[a][b] = G[b][a] = ;
}
c[] = ;
DFS(, );//将1号染成黑色
if(f == )
printf("No\n");
else
{
int ans = ;
memset(used, , sizeof(used));
for(i = ; i<= n ; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
printf("%d\n", ans / );
}
}
return ;
}

hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)的更多相关文章

  1. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  2. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

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

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

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

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

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

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

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

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

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

随机推荐

  1. UVa 714 (二分) Copying Books

    首先通过二分来确定这种最大值最小的问题. 假设每个区间的和的最大值为x,那么只要判断的时候只要贪心即可. 也就是如果和不超过x就一直往区间里放数,否则就开辟一个新的区间,这样来判断是否k个区间容得下这 ...

  2. hdu4604 deque

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 思路:就是模拟一下,求每一个开始的非上升和非下降序列.然后求重复的数,由于求出来可能不会是我们想 ...

  3. 堆Heap

    #pragma once#include <vector> // 小堆template<class T>  //仿函数struct Less{       bool opera ...

  4. 甚是挂念学校的acmer

    虽然自己一直不承认.. 今天头疼,不想工作,况且自己服务端代码也写差不多了,于是又干起了自己的本行,去信息站找退役帖看,又把3xian的文章看了一遍,这次我从文章里面读到的更多的是懊恼,恨铁不成钢.经 ...

  5. dede 替换后台两个文件去广告

    A:替换后台两个文件去广告  dede/templets路径下两个文件 1.index2.htm <!--This is IE DTD patch , Don't delete this lin ...

  6. HDU Sky数 2097

    解题思路:类比求出10进制数各个位上的数字之和,求出12进制和16进制上的数. #include<cstdio> #include<cstring> #include<a ...

  7. 【英语】Bingo口语笔记(76) - 不知如何应答的场景对话

  8. 用list<类>集合接收一个网址返回的一个类的集合的XML

    JavaScriptSerializer serializer = new JavaScriptSerializer(); string json = Share.Helper.HttpRequest ...

  9. AsciiDoc

    AsciiDoc Text based document generation AsciiDoc Home Page Table of Contents Introduction Overview a ...

  10. WeifenLuo.WinFormsUI.Docking"的使用 z

    在伍华聪的博客中,看到布局控件"WeifenLuo.WinFormsUI.Docking",发现的确是一个非常棒的开源控件,用过的人都深有体会,该控件之强大.美观.不亚于商业控件. ...