题目大意:有一群人他们有一些关系,比如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. Change value of string array at debug eclipse--转

    Question: I have an application, but to test something, I need to change value of a String[]. But wh ...

  2. [转] Form表单中method="post/get'的区别

    Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一 ...

  3. JDK5-静态导入

    import static 1. 导入一个类内所有静态成员 import static java.lang.Math.*; public class StaticImport { public sta ...

  4. 判断textview是否被截断

    Layout l = textview.getLayout(); if ( l != null){ int lines = l.getLineCount(); if ( lines > 0) i ...

  5. jQuery代码优化 事件委托篇

    <转自 http://www.jb51.net/article/28770.htm> 参考文章:  解密jQuery事件核心 - 绑定设计(一) 参考文章:  解密jQuery事件核心 - ...

  6. vertical-align各属性对比

    测试用代码 <!DOCTYPE html> <html> <head> <style> #dd { //line-height: 300px; back ...

  7. Dhroid框架配置

    1.将dhroid文件夹作为一个Module导入,dhroid下载地址 2.在build.gradle中的dependencies节点中添加compile project(':dhroid') dep ...

  8. leetcode修炼之路——350. Intersection of Two Arrays II

    题目如下: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2 ...

  9. Lucene.net 从创建索引到搜索的代码范例

    关于Lucene.Net的介绍网上已经很多了在这里就不多介绍Lucene.Net主要分为建立索引,维护索引和搜索索引Field.Store的作用是通过全文检查就能返回对应的内容,而不必再通过id去DB ...

  10. 虚拟器运行iOS8地图提示错误

    /SourceCache/ProtocolBuffer_Sim/ProtocolBuffer-225/Runtime/PBRequester.m:799 server (https://gsp13-c ...