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. [ionic开源项目教程] - 第14讲 ionic解决跨域问题

    [ionic开源项目教程] 第14讲 使用jsonp解决跨域问题 相信很多朋友在开发中都会遇到以下这个问题. No 'Access-Control-Allow-Origin' header is pr ...

  2. UVa 1609 (博弈) Foul Play

    姑且把它归类为一道博弈吧,毕竟这也是在找必胜方案. 十分有意思的一道题目,设计一种方案让你支持的1队获胜. 题目给出了两个很重要的条件: 1队能打败至少一半的队伍 对于1队不能打败的黑队,一定存在一个 ...

  3. HDU 2059 龟兔赛跑

    受上一道题影响,我本来想着开一个二维数组来表示充电和不充电的状态. 可这样就有一个问题,如果没有充电,那么在下一个阶段就有剩余的电量. 这样问题貌似就不可解了,难道是因为不满足动态规划的无后效性这一条 ...

  4. 进程描述符task_struct

    1.进程状态 volatile long state; int exit_state; state成员的可能取值如下: #define TASK_RUNNING        0 #define TA ...

  5. impersonate a user

    // This sample demonstrates the use of the WindowsIdentity class to impersonate a user. // IMPORTANT ...

  6. 给产品经理讲技术,不得不懂的TCP和UDP

    TCP/IP协议,你一定经常听说吧,其中TCP(Transmission Control Protocol)称为传输控制协议,IP(Internet Protocol)称为因特网互联协议,好吧,这都是 ...

  7. 四:分布式事务一致性协议paxos通俗理解

    转载地址:http://www.lxway.com/4618606.htm 维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的"La" ...

  8. JAVA虚拟机内存分配与回收机制

    Java虚拟机(Java Virtual Machine) 简称JVM Java虚拟机是一个想象中的机器,在实际的计算机上通过软件模拟来实现.Java虚拟机有自己想象中的硬件,如处理器.堆栈.寄存器等 ...

  9. 【转】Linux高级字符设备之Poll操作

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275559.html 在用户程序中,select()和poll()也是与设备阻塞与非阻塞 ...

  10. 【转】lua Date和Time

    time和date两个函数在Lua中实现所有的时钟查询功能.函数time在没有参数时返回当前时钟的数值.(在许多系统中该数值是当前距离某个特定时间的秒数.)当为函数调用附加一个特殊的时间表时,该函数就 ...