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可能存在图中没一个点:将该问题转换为图论问题 ...
随机推荐
- 关于axios及其在vue中的配置
什么是axios?官方解释:axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 具有以下特点: 从浏览器中创建 XMLHttpRequests 从 nod ...
- iOS开发-从16进制颜色中获取UIColor
目前iOS中设置UIColor只能使用其枚举值.RGB等方法,不能直接将常用的16进制颜色值直接转为UIColor对象,所以写了点代码,将16进制颜色值转为UIColor. 代码如下, //头文件#i ...
- [Umbraco] Data Type之Render control
继续探讨Data Type.如果你创建过Data Type,你就会知道创建一个新的Data Type都需要指定一个Render control,这有点类似开始C#时用到的继承. 那么如何创建我们自己的 ...
- web3调用call()方法获取不到返回值
一.web3的call()获取不到返回值问题和解决方法 在彩票小合约中,遇到一个问题:合约中 有两个方法 第一个返回一个账户地址,没有使用到当前方法调用者信息: 第二个使用到了当前方法调用者信息 在w ...
- 本机spark 消费kafka失败(无法连接)
本机spark 消费kafka失败(无法连接) 终端也不报错 就特么不消费: 但是用console的consumer 却可以 经过各种改版本 ,测试配置,最后发现 只要注释掉 kafka 配置se ...
- Mybatis 事务管理和缓存机制
一级缓存--SqlSession级别 数据库表tb_user User package com.example.demo.domain; public class User { private Int ...
- 如何查看第三方apk的信息
很多时候,我们需要获取别人的apk的信息.但是我们看不到apk的代码,对于apk的信息并没有直接的方法获取.那么,我们要怎么获取apk信息呢? 这里,我整理了两个方法,亲测可用. 第一种,直接使用An ...
- 一学期积累下来的SQL语句写法的学习
整合了一下上学期学习的积累,希望可以帮到初学者! 可能以后会有用吧! A 基本语句的运用 操作基于emp表1.按工资从高到低排列SQL> select rownum as 次序,ename,sa ...
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(八):MyBatis分页功能实现
使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写 count 和 select,幸 ...
- CentOS 6.9上inotify-tools 安装及使用方法
文章目录 [隐藏] 一.检查系统内核版本 三.下载安装(下载有点慢) 四.查看inotify默认参数 五.修改inotify参数 六.创建实时监控脚本 (file 里面放的需要监听的目录) 七:实例操 ...