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

Problem Description
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

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
For each data set:
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
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
 
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
 
Sample Output
No
3
题意:一些学生存在认识与不认识的关系,但这种关系不传递。问能否将学生分为两组,每组中的学生互相不认识(即二分图判定)。若可以,把相互认识的同学放在一个双人间,问最多需要几个双人间(即二分图最大匹配)。
#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(二分图判定+最大匹配)的更多相关文章

  1. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  2. HDU2444(KB10-B 二分图判定+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

    这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

  4. HDU2444 【二分图判定+最大匹配】

    套模板很好的题? #include<bits/stdc++.h> using namespace std; const int N=2e2+10; const int M=4e4+10; ...

  5. CF687A. NP-Hard Problem[二分图判定]

    A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. 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/ ...

  7. POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)

    题意 现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能 ...

  8. COJ 0578 4019二分图判定

    4019二分图判定 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个具有n个顶点(顶点编号为0,1,… ...

  9. hdoj 3478 Catch(二分图判定+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...

随机推荐

  1. python_获得列表中重复的项的索引

    a = ['b','a', 'b', 'c', 'a', 'c','d'] b=[] f=[] for i in a: c=[] for item in enumerate(a): if item[1 ...

  2. 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...

  3. 笔记本POWER部分的应用——(MOS/LDO/BUCK BOOST)

    一.MOSFET 简介: 金属-氧化物半导体场效应晶体管,简称金氧半场效晶体管(Metal-Oxide-Semiconductor Field-Effect Transistor, MOSFET)是一 ...

  4. upstart man

    man upstart nit(8) init(8) NAME init - Upstart process management daemon SYNOPSIS init [OPTION]... D ...

  5. NAT和桥接的区别

    NAT 桥接 NAT相当于是局域网中的局域网,把192.168.21.1当作外网ip,重新划分了一个网关(192.168.33.x) 网桥只是把网络桥接起来,还是原来的网关(192.168.21.x) ...

  6. Spring Boot实现STOMP协议的WebSocket

    关注公众号:锅外的大佬 每日推送国外优秀的技术翻译文章,励志帮助国内的开发者更好地成长! WebSocket协议是应用程序处理实时消息的方法之一.最常见的替代方案是长轮询(long polling)和 ...

  7. 使用Auto Layout处理比例间距问题

    使用Auto Layout处理比例间距问题 Auto Layout 是一个掌握起来很具有挑战性的东西.iOS 9引入的 Stack Views和 layout 锚点有一些帮助,但是明白如何创建特定的 ...

  8. iOS开发 - UIViewController控制器管理

    创建一个控制器 控制器常见的创建方式有下面几种 //通过storyboard创建 //直接创建 ViewController *vc = [[ViewController alloc] init]; ...

  9. React项目结构

    任何一种语言.框架,在真正上手的时候,多多少少会想想怎么安排项目结构(正所谓磨刀不误砍柴工),React也不例外. google了下,拿下面3篇博客来说道说道. (1) how-to-better-o ...

  10. Android自动折行TextView Group

    package com.test.testview; import java.util.ArrayList; import android.content.Context; import androi ...