HDU2444(KB10-B 二分图判定+最大匹配)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6983 Accepted Submission(s): 3120
Problem Description
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.
Input
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.
Output
Sample Input
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
Sample Output
3
Source
//2017-08-21
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
const int N = ;
int head[N], tot;
struct Edge{
int to, next;
}edge[N*N]; void init(){
tot = ;
memset(head, -, sizeof(head));
} void add_edge(int u, int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} int n, m;
string G[N];
int matching[N];
int check[N]; bool dfs(int u){
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!check[v]){//要求不在交替路
check[v] = ;//放入交替路
if(matching[v] == - || dfs(matching[v])){
//如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功
matching[u] = v;
matching[v] = u;
return true;
}
}
}
return false;//不存在增广路
} //hungarian: 二分图最大匹配匈牙利算法
//input: null
//output: ans 最大匹配数
int hungarian(){
int ans = ;
memset(matching, -, sizeof(matching));
for(int u = ; u <= n; u++){
if(matching[u] == -){
memset(check, , sizeof(check));
if(dfs(u))
ans++;
}
}
return ans;
} int col[N]; //color: 黑白染色二分图判定
//output: true 是二分图, false 不是二分图
bool color(int u){
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!col[v]){
col[v] = !col[u];
if(!color(v))return false;
}else if(col[v] == col[u])
return false;
}
return true;
} int main()
{
//freopen("inputB.txt", "r", stdin);
while(scanf("%d%d", &n, &m)!=EOF){
//考虑只有一个人时的特殊情况
if(n == ){
printf("No\n");
continue;
}
init();
int u, v;
for(int i = ; i < m; i++){
scanf("%d%d", &u, &v);
add_edge(u, v);
}
memset(col, , sizeof(col));
col[] = ;
if(color())
printf("%d\n", hungarian());
else
printf("No\n");
} return ;
}
HDU2444(KB10-B 二分图判定+最大匹配)的更多相关文章
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- HDU2444(二分图判定+最大匹配)
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可能存在图中没一个点:将该问题转换为图论问题 ...
随机推荐
- cad.net之ACAD移植到GCAD的自动加载问题
将acad.pgp,lsp,fas,vlx,名称增加一份gcad.pgp,lsp,fas,vlx.涉及系统加载用. Lisp的拖拉加载在gcad无法通过lastprompt获取命令历史栏最后一行(含路 ...
- GoLang学习控制语句之字符串
Go语言字符串是一种值类型,且值不可变,即创建某个文本后你无法再次修改这个文本的内容:更深入地讲,字符串是字节的定长数组.Go 代码使用 UTF-8 编码(且不能带 BOM),同时标识符支持 Unic ...
- Linux 学习错误点整理之网络配置
本人是一名实习生,最近在学习Linux,在实操的过程中还是遇到了一些问题,所以想记录下来,供自己以后复习,也希望能给跟我一样的菜鸟的人带来一点点帮助. 我用的是VMware Workstation P ...
- dotnetcore+vue+elementUI 前后端分离 三(前端篇)
说明: 本项目使用了 mysql employees数据库,使用了vue + axois + element UI 2.0 ,演示了 单页程序 架构 ,vue router 的使用,axois 使用, ...
- (转)CentOS7安装Nginx1.14.2
原文:https://blog.csdn.net/zhyfyz/article/details/84957381 https://blog.csdn.net/q85795362/article/det ...
- (转)python3-staticmethod与classmethod
原文:https://blog.csdn.net/youngbit007/article/details/68957848 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- webgl之观察三维空间
在之前的教程中,我们已经接触到了3d的基本应用,而这里,将会继续介绍两种不同的相机,即透视相机和正投影相机:还会学习设置相机的不同参数,这样就可以使场景以不同的角度显示出来. 一.正投影和透视投影概念 ...
- Java 并发优化
线程不安全 SimpleDateFormat不是线程安全的 SimpleDateThread import java.text.ParseException; import java.text.Sim ...
- Android 安装时报错INSTALL_FAILED_NO_MATCHING_ABIS 的解决办法
$ adb push D:\AndroidstudioTestWorkSpace\app\build\outputs\apk\app-debug.apk /data/local/tmp/c ...
- 前端通信:ajax设计方案(五)--- 集成promise规范,更优雅的书写代码(改迭代已作废,移步迭代10)
该迭代已作废,最新的请移步这里:https://www.cnblogs.com/GerryOfZhong/p/10726306.html 距离上一篇博客书写,又过去了大概几个月了,这段时间暂时离开了这 ...