HDU2444(二分图判定+最大匹配)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4250 Accepted Submission(s): 1946
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.
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.
#include"cstdio"
#include"cstring"
#include"vector"
#include"algorithm"
using namespace std;
const int MAXN=;
vector<int> G[MAXN];
int V,E;
bool bfs(int s)//二分图判断
{
int que[MAXN],rear=,front=;
int color[MAXN];
memset(color,-,sizeof(color));
color[s]=;que[rear++]=s;
while(rear!=front)
{
int v=que[front++];
for(int i=;i<G[v].size();i++)
{
int u=G[v][i];
if(color[u]==-)
{
color[u]=!color[v];//若没有染色则染成相反颜色
que[rear++]=u;
}
else if(color[u]==color[v]) return false;//若相邻结点颜色相同则不是二分图
}
}
return true;
}
int match[MAXN];
int vis[MAXN];
bool dfs(int u)//二分图匹配
{
vis[u]=;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i],w=match[v];
if(w==||(!vis[w]&&dfs(w)))
{
match[u]=v;
match[v]=u;
return true;
}
}
return false;
}
int matching()
{
memset(match,,sizeof(match));
int ans=;
for(int i=;i<=V;i++)
{
if(!match[i])
{
memset(vis,,sizeof(vis));
if(dfs(i)) ans++;
}
}
return ans;
}
int main()
{
while(scanf("%d%d",&V,&E)!=EOF)
{
for(int i=;i<=V;i++) G[i].clear();
for(int i=;i<E;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
if(bfs()) printf("%d\n",matching());
else printf("No\n");
}
return ;
}
HDU2444(二分图判定+最大匹配)的更多相关文章
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- HDU2444(KB10-B 二分图判定+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
- HDU2444 【二分图判定+最大匹配】
套模板很好的题? #include<bits/stdc++.h> using namespace std; const int N=2e2+10; const int M=4e4+10; ...
- CF687A. NP-Hard Problem[二分图判定]
A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- hdu3729 I'm Telling the Truth (二分图的最大匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/ ...
- POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)
题意 现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能 ...
- COJ 0578 4019二分图判定
4019二分图判定 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个具有n个顶点(顶点编号为0,1,… ...
- hdoj 3478 Catch(二分图判定+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...
随机推荐
- java 发送微信客服消息
package com.baosight.wechat.service; import net.sf.json.JSONObject; import org.apache.commons.httpcl ...
- POJ2376 Cleaning Shifts 【贪心】
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11542 Accepted: 3004 ...
- CrtmpServer 接收推送视频流 注册流基本流程
今天研究了CrtmpServer 将客户端推动过来的视频流注册到服务的流程,记录下来,以备后用. 图1 注册前端视频流流程
- C# 将cookie写入WebBrowser
string cookie = ""; foreach (string c in cookie.Split(';')) { string[] item = c.Split('=') ...
- CUDA vs GPGPU
http://www.kuqin.com/hardware/20080830/15726.html 一 在硬件和软件之间选择 “计算机软件和硬件的逻辑等价性”是计算机组成原理中一个非常重要的理 ...
- 升级OpenSSL修复高危漏洞Heartbleed
升级OpenSSL修复高危漏洞Heartbleed 背景: OpenSSL全称为Secure Socket Layer.是Netscape所研发.利用数据加密(Encryption) ...
- Linux快捷键和vim快捷键
系统下常用快捷键 ctrl+左右键 在单词之间跳转 Ctrl + a 光标移动到行首(ahead of line),相当于通常的Home键 Ctrl + e ...
- mac下执行文件出现Permission Denied的解决
mac 下终端访问文件出现“Permission Denied”解决方案: 一个文件有3种权限,读.写.可执行,你这个文件没有可执行权限,需要加上可执行权限. 1. 终端下先 cd到该文件的目录下 2 ...
- php 静态成员(static)抽象类(abstract)和接口(interface)
首先看一下静态成员(static)和普通成员(public; protect; private)的区别: 静态成员是属于类的,普通成员是属于对象的: 例如: <?php header(" ...
- 九度OJ 1091:棋盘游戏 (DP、BFS、DFS、剪枝)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1497 解决:406 题目描述: 有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代 ...