这是一个基础的二分图,题意比较好理解,给出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. XBOX360 硬盘玩游戏

    首先PC端需要到flashFXP这个软件,因为级别不够上传不了就发个下载网站各位自己下一下吧.http://www.oyksoft.com/soft/14875.html#oyksoftdown 好了 ...

  2. jfreechart 实例

    http://blog.csdn.net/ami121/article/category/394379 jfreechart实例(三)股价K线波动图 package com.ami;/** *@ Em ...

  3. boolean类型相关

    如果逻辑对象无初始值或者其值为 0.-0.null."".false.undefined 或者 NaN,那么对象的值为 false.否则,其值为 true(即使当自变量为字符串 & ...

  4. HDU2535:Vote

    Problem Description 美国大选是按各州的投票结果来确定最终的结果的,如果得到超过一半的州的支持就可以当选,而每个州的投票结果又是由该州选民投票产生的,如果某个州超过一半的选民支持希拉 ...

  5. 运行出第一个程序Hello World、第二个程序网页浏览器

    很长时间没有发博客记录我的奋斗历程了,原因不外乎遇到了对我而言“巨大的”困难. 经历了长期的找不到合适的教材.找不到Xcode资源.运行不出例程的痛苦以后,近日终于走上正轨. 现在补发一个过去2个月来 ...

  6. NSArray的containsObject漏洞

    1.NSArray中的containsObject的用法 NSMutableArray *array=[NSMutableArray array]; if(![array containsObject ...

  7. Bookmark

    http://stackoverflow.com/https://www.baidu.com/?tn=06074089_27_pghttp://apistore.baidu.com/http://to ...

  8. Hibernate 系列教程17-查询缓存

    在二级缓存配置成功的基础上进行查询缓存配置 Product public class Product { private Long id; private String name; Product.h ...

  9. Android ViewDragHelper完全解析 自定义ViewGroup神器

    Android ViewDragHelper完全解析 自定义ViewGroup神器   转载请标明出处: http://blog.csdn.net/lmj623565791/article/detai ...

  10. Android Camera(一)

    最近老大交给了一个任务,说是要在本地视频端很够调节摄像头焦距. 碰到了一些问题: 1.手机支不支持摄像头变焦 2.系统自带摄像软件可以变焦,但是自己编写的程序不支持变焦, 这个问题网上也有很多童鞋碰到 ...