HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3775 Accepted Submission(s): 1771
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.
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
3
/* ***********************************************
Author :pk28
Created Time :2015/10/6 19:53:21
File Name :hdu2444.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 40000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std; bool cmp(int a,int b){
return a>b;
}
int n;
int m,l,tot;
int pre[],match[];
int vis[maxn]; struct node{
int v,next;
}edge[maxn];
void init(){
l=;
memset(pre,-,sizeof pre);
memset(match,-,sizeof match);
}
void add(int u,int v){
edge[l].v=v;
edge[l].next=pre[u];
pre[u]=l++;
}
int dfs(int u){
for(int i=pre[u];i+;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]){
vis[v]=;
if(match[v]==-||dfs(match[v])){
match[v]=u;
return ;
}
}
}
return ;
}
int hungary(){
tot=;
for(int i=;i<=n;i++){
cle(vis);
if(dfs(i))tot++;
}
return tot;
}
int color[maxn];
bool bs(int u){
for(int i=pre[u];i+;i=edge[i].next){
int v=edge[i].v;
if(color[v]==color[u])return false;
if(!color[v]){
color[v]=-color[u];
if(!bs(v))return false;
}
}
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int a,b;
while(cin>>n>>m){
init(); for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
cle(color);
color[]=;
if(!bs())puts("No");
else printf("%d\n",hungary()/);
}
return ;
}
HDU 2444 The Accomodation of Students(判断二分图+最大匹配)的更多相关文章
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 2444 The Accomodation of Students【二分图最大匹配问题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...
- (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
[题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...
- hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配
此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
- 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 ...
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
随机推荐
- 【kmp或扩展kmp】HDU 6153 A Secret
acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...
- 【bzoj1878】[SDOI2009]HH的项链 - 树状数组 - 离线处理
[SDOI2009]HH的项链 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4834 Solved: 2384[Submit][Status][Dis ...
- 垃圾收集器与内存分配策略 (深入理解JVM二)
1.概述 垃圾收集(Garbage Collection,GC). 当需要排查各种内存溢出.内存泄露问题时,当垃圾收集成为系统达到更高并发量的瓶颈时,我们就需要对这些“自动化”的技术实施必要的监控和调 ...
- MongoDB_语法命令
可以通过MongoDB shell 来连接MongoDB服务: ./mongo 进入交互 数据库-->集合-->文档 几个文档就组成了集合,可以设置固定大小的集合,集合就会有过期机制, ...
- flowable资料
flowable-modeler画流程图:https://www.youtube.com/watch?v=0pqD50pySJg 这个视频跳过创建用户那一步了:https://www.youtube. ...
- codevs——1570 去看电影
1570 去看电影 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 农夫约翰带着他的一些奶牛去看电影.而他的 ...
- TYVJ P1577 泥泞的道路
题目链接:http://www.tyvj.cn/p/1577# P1577 泥泞的道路 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 公园中有n个景点,编号 ...
- 深入GCD(二): 多核心的性能
概念为了在单一进程中充分发挥多核的优势,我们有必要使用多线程技术(我们没必要去提多进程,这玩意儿和GCD没关系).在低层,GCD全局dispatch queue仅仅是工作线程池的抽象.这些队列中的Bl ...
- C# 把控件内容导出图片
Bitmap newbitmap = new Bitmap(panelW.Width, panelW.Height); panelW.DrawToBitmap(newbitmap ...
- 向C#的选项卡中添加自定义窗体
一.自定义窗体的搭建 这个比较简单,添加一个WinForm窗体就行了,设置一个名字EditPanel,然后在窗体上画需要的控件. 二.将自定义窗体添加到选项卡 // 新建窗体加入到选项卡中 EditP ...