题目大意:有一群人他们有一些关系,比如A认识B, B认识C, 但是这并不意味值A和C认识。现在给你所有互相认识的学生,你的任务是把所有的学生分成两个一组,
住在一个双人房里。相互认识的同学可以住在一个双人房里。
输入数据:
有n个学生 m个关系(m对是相互认识的)
接下来m行是,是m个关系。
如果能够匹配成功则输出需要双人房的个数,否则输出'No'
 
思路:先判断是否是个二分图,可以使用黑白染色的方法来判断。然后再进行最大匹配。
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 500
bool G[maxn][maxn], vis[maxn];
int color[maxn], P[maxn];///黑白染色 -1 黑色 1 白色
int n, m; bool DFS(int u,int c)///判断是否是二分图,黑白染色
{
color[u] = c;
for(int i=; i<=n; i++)
{
if(!G[u][i] )
continue;
if(color[i] == )
{
if( DFS(i, -c) )
continue;
return false;
}
else if(color[i] + color[u])
return false; }
return true;
}
bool Find(int u)
{
for(int i=; i<=n; i++)
{
if(G[u][i] && !vis[i])
{
vis[i] = true;
if(P[i] == - || Find(P[i]))
{
P[i] = u;
return true;
}
}
}
return false;
} int solve()
{
bool ok;
for(int i=; i<=n; i++)
{
if(color[i] == )
ok = DFS(i, );
if(ok == false)
return -;
} int ans = ;
for(int i=; i<=n; i++)
{
memset(vis, false, sizeof(vis));
if(color[i] && Find(i))
ans ++;
}
return ans;
} int main()
{
while(scanf("%d %d",&n, &m) != EOF)
{
int a, b;
memset(G, false, sizeof(G));
memset(P, -, sizeof(P));
memset(color, , sizeof(color)); for(int i=; i<m; i++)
{
scanf("%d %d",&a, &b);
G[a][b] = true;
}
int ans = solve(); if(ans == -)
puts("No");
else
printf("%d\n", ans );
}
return ;
}

HDU 2444 The Accomodation of Students(判断是否可图 + 二分图)的更多相关文章

  1. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

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

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

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

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

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

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

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

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

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

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

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

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

  9. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

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

随机推荐

  1. Android中View和ViewGroup介绍

    1. 概念Android中的View与我们以前理解的“视图”不同.在Android中,View比视图具有更广的含义,它包含了用户交互和显示,更像Windows操作系统中的window. ViewGro ...

  2. 精准化测试专业平台Paw:苹果APP应用代码质量的守护者

    众所周知,一款苹果APP进入苹果应用商店后,要上排行榜,要被首页推荐,版本更新必不可少,但作为版本更新的关键所在,软件测试一直被业内吐槽“坑太多”,缺陷防不胜防.相关经验缺乏等等.若问如何“填坑”?以 ...

  3. 解决下载android sdk慢的问题

    修改host文件 203.208.46.146 dl.google.com 203.208.46.146 dl-ssl.google.com 强制不使用https访问 在sdk manager里选择t ...

  4. Apache MINA 框架之Handler介绍

    IoHandler 具备以下几个功能: sessionCreated sessionOpened sessionClosed sessionIdle exceptionCaught messageRe ...

  5. 移动端屏幕自适应js与rem

    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;&qu ...

  6. linux command cp.

    Examples cp file1.txt newdir Copies the file1.txt in the current directory to the newdir subdirector ...

  7. jquery动画总结

    基本动画 show() //直接显示元素,没有动画 show(speed, [callback]) //有动画,有回调函数 hide() //直接隐藏元素,没有动画 hide(speed, [call ...

  8. [JS] save txt file

    (function () { var blob = new Blob(['content'], {type: 'text/plain; charset=utf-8'}), blobUrl = URL. ...

  9. CSS3重要内容翻译

    以上是废话 1.3    此处未完全确认,相较于css3和css3的选择器,区别包括: 基础定义改变(选择器.选择器组,简单选择器等),特别的,作为css2中简单选择器,如今被成为简单选择器序列,“简 ...

  10. Linux makefile 教程 非常详细,且易懂(转)

    转自:http://blog.chinaunix.net/uid-27717694-id-3696246.html 原文地址:Linux makefile 教程 非常详细,且易懂 作者:Deem_pa ...