这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以判断,其次让我们求分在两部分的最大对数,这就是二分图的最大匹配问题,这里数据只有200,所以匈牙利算法求蹭广路径的办法可以解决这个问题,也相对比较容易编写。

另外一开始我链式前向星的数组开小了,G++居然返回超时,后来换了C++才RE,想到数组越界的问题,不得不说这些编译器真傲娇啊~

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
struct EDGE
{
int to,nxt;
}edge[];
int head[],n,m,tot;
void add_edge(int x,int y)
{
edge[tot].to = y;
edge[tot].nxt = head[x];
head[x] = tot++;
}
int clo[];
bool bfs(int id)
{
queue<int>que;
clo[id] = ;
while(!que.empty()) que.pop();
que.push(id);
while(!que.empty())
{
int now = que.front();
que.pop();
for(int i = head[now];i != -;i = edge[i].nxt)
{
int go = edge[i].to;
if(clo[go] == -)
{
clo[go] = (clo[now] ^ );
que.push(go);
}
else if(clo[go] == clo[now]) return false;
}
}
return true;
}
bool is_erfen()
{
memset(clo,-,sizeof(clo));
bool mark = true;
for(int i = ;i <= n;i++)
{
if(clo[i] != -)continue;
mark = bfs(i);
if(mark == false) return false;
}
return mark;
}
int vis[],link[];
bool Find(int x)
{
for(int i = head[x];i != -;i = edge[i].nxt)
{
int now = edge[i].to;
if(vis[now]) continue;
vis[now] = ;
if(link[now] == - || Find(link[now]))
{
link[now] = x;
return true;
}
}
return false;
}
int match()
{
memset(link,-,sizeof(link));
int ans = ;
for(int i = ;i <= n;i++)
{
memset(vis,,sizeof(vis));
if(Find(i))
ans++;
}
return ans / ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
tot = ;
memset(head,-,sizeof(head));
for(int i = ;i < m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
bool flag = is_erfen();
if(!flag)
{
printf("No\n");
}
else printf("%d\n",match());
}
return ;
}

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

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

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

  2. HDU 2444 The Accomodation of Students二分图判定和匈牙利算法

    本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...

  3. 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 ...

  4. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  5. HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)

    题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...

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

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

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

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

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

随机推荐

  1. 使用httperrequest,模拟发送及接收Json请求

    使用httpreques\Json-Handle\tcpdump\wireshark工具进行,抓取手机访问网络的包,分析request及response请求,通过httprequester来实现模拟发 ...

  2. prestashop 基本设置

    不跳转 要先改这个UPDATE`ps_shop_url`SET`domain_ssl`= replace(`domain_ssl` ,'us2.try.net','www.gelchaussures. ...

  3. URLEncode和URLDecoder作用

    一.基本原理 对于URL传递到后台,会对其中的有些字符进行编码,以下是我百度到的一些资料. 网页中的表单使用POST方法提交时,数据内容的类型是 application/x-www-form-urle ...

  4. hdu_4714_Tree2cycle(树形DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给你N个点N-1条边,形成一个树,让你拆树,并连接成一个环,每拆一次,连接一次,消耗1,问 ...

  5. 自定义 IP 地址

    可以在安装的时候,点击网络配置 1.修改网卡配置 编辑:vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #描述网卡对应的设备别名,例如 ...

  6. how to use tar?

    In UNIX, tar is the most useful tool to compress files (just like zip in Windows.) To compress, inpu ...

  7. 数据库sql语句为什么要用绑定形式?

    基于两点: 1,安全性,防sql注入: 2,共享资源,相似的sql能被缓存而不是重新解析. 淘测试给出了一个很好的理由:http://www.taobaotesting.com/blogs/859

  8. javascript操作json

    for (var i = 0; i < selectedPartList.length; i++) { if (selectedPartList[i].vpart_code == jsonRow ...

  9. 【Holograms 101D】一步步用Unity 开发 Hologram

    转载请注明出处: copperface:[Holograms 101D]一步步用Unity 开发 Hologram Holograms 101 该教程将带领你走完 Hologram 创建 的全过程.整 ...

  10. 前台改变asp button控件的值,后台取值没有改变的问题

    前台: <asp:Button ID="btnEdit" Style="margin-left: 600px;" runat="server&q ...