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可能存在图中没一个点:将该问题转换为图论问题 ...
随机推荐
- C语言实现windows进程遍历
#include <windows.h> #include <tlhelp32.h> //进程快照函数头文件 #include <stdio.h> int main ...
- Spring中ioc的实现原理
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- vue+iview实现一行平均五列布局
iview 的栅格布局是以 html代码部分: <Row :gutter="20"> <Col style="float: left;width: 20 ...
- 设置Jmeter默认为中文, 就是这么简单!
Jmeter默认加载的全英文,想要看的更加明白,想到的就是汉化了. Jmeter汉化真的非常简单,意料之外的简单,只需要到配置文件 jmeter.properties ,将里面的 “#language ...
- C# 添加修改防火墙端口及程序
文章转自:http://sdfiyon.iteye.com/blog/1197511 一.添加 COM 引用 在引用里,选择 COM 页, 找到 NetFwTypeLib , 确定即可 二.添加允许通 ...
- 剑指offer十三之调整数组顺序使奇数位于偶数前面
一.题目 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 二.思路 此题可 ...
- 理解web service 和 SOA
什么是SOA? SOA的全称为Service Oriented Architecture,即面向服务架构.这是一种架构理念.它的提出是在企业计算领域将耦合的系统划分为松耦合的无状态的服务.服务发布出来 ...
- ubuntu-12.04.5下编译openjdk8
bash ./configure --with-target-bits=64 --with-boot-jdk=/usr/java/jdk1.7.0_80/ --with-debug-level=slo ...
- JavaScript -- Enumerator
-----022-Enumerator.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&q ...
- Java判断一个时间是否在时间区间内
package com.liying.tiger.test; import java.text.ParseException; import java.text.SimpleDateFormat; i ...