The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8418    Accepted Submission(s): 3709

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
 
Source
 
Recommend
gaojie

先判断能不能分成二分图 , 不是就输出No。。只有1个的时候也不是

然后求完美匹配即可。。。我不会写匈牙利了。。。。只会写hk。。。还是套模板。。。

因为没有分左右  所以要除2

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int dx[maxn], dy[maxn], cx[maxn], cy[maxn], used[maxn], vis[maxn];
int nx, ny, dis;
vector<int> G[];
int n, m;
int bfs()
{
queue<int> Q;
dis = INF;
mem(dx, -);
mem(dy, -);
for(int i=; i<=nx; i++)
{
if(cx[i] == -)
{
Q.push(i);
dx[i] = ;
}
}
while(!Q.empty())
{
int u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(int v=; v<G[u].size(); v++)
{
int i = G[u][v];
if(dy[i] == -)
{
dy[i] = dx[u] + ;
if(cy[i] == -) dis = cy[i];
else
{
dx[cy[i]] = dy[i] + ;
Q.push(cy[i]);
}
}
}
}
return dis != INF;
} int dfs(int u)
{
for(int v=; v<G[u].size(); v++)
{
int i=G[u][v];
if(!used[i] && dy[i] == dx[u] + )
{
used[i] = ;
if(cy[i] != - && dis == dy[i]) continue;
if(cy[i] == - || dfs(cy[i]))
{
cy[i] = u;
cx[u] = i;
return ;
}
}
}
return ;
} int hk()
{
int res = ;
mem(cx, -);
mem(cy, -);
while(bfs())
{
mem(used, );
for(int i=; i<=nx; i++)
if(cx[i] == - && dfs(i))
res++;
}
return res;
} int istwo(int u)
{
queue<int> E;
mem(vis, -);
E.push(u);
vis[u] = ;
while(!E.empty())
{
u = E.front(); E.pop();
for(int i=; i<G[u].size(); i++)
{
int v = G[u][i];
if(vis[v] == -)
{
if(vis[u] == ) vis[v] = ;
else vis[v] = ;
E.push(v);
}
else if(vis[v] == vis[u])
return ;
}
}
return ;
} int main()
{
while(cin>> n >> m && n+m)
{
for(int i=; i<maxn; i++) G[i].clear(); for(int i=; i<m; i++)
{
int u, v;
cin>> u >> v;
G[u].push_back(v);
G[v].push_back(u);
} if(!istwo() || n == )
{
cout<< "No" <<endl;
continue;
}
nx = n; ny = n;
cout<< hk()/ <<endl; } return ;
}

The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)的更多相关文章

  1. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

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

  2. The Accomodation of Students HDU - 2444 二分图判定 + 二分图最大匹配 即二分图-安排房间

    /*655.二分图-安排房间 (10分)C时间限制:3000 毫秒 |  C内存限制:3000 Kb题目内容: 有一群学生,他们之间有的认识有的不认识.现在要求把学生分成2组,其中同一个组的人相互不认 ...

  3. (匹配)The Accomodation of Students --HDU --2444

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2444 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. B - The Accomodation of Students - hdu 2444(最大匹配)

    题意:现在有一些学生给你一下朋友关系(不遵守朋友的朋友也是朋友),先确认能不能把这些人分成两组(组内的人要相互不认识),不能分的话输出No(小写的‘o’ - -,写成了大写的WA一次),能分的话,在求 ...

  5. HDU——2444The Accomodation of Students(BFS判二分图+最大匹配裸题)

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

  6. hdu 1281 棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  8. hdu 1045 Fire Net(二分匹配 or 暴搜)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. hdu 2444 交叉染色判断二分图+二分最大匹配

    /*1A 31ms*/ #include<stdio.h> #include<string.h> #define N 300 int n; struct node { int ...

随机推荐

  1. 关于MSCOCO_text数据集的探索

    最近需要做图片中文本识别的项目,然后为了快速验证模型,所以找到了mscoco-text数据集,网站1上是这么说的: 官网是这么说的: 然而,我下下来之后: 1 - 先导入: 2 - 其中key为'im ...

  2. Scala _ 下划线

    1.引入包中的全部方法 import math._ //引入包中所有方法,与java中的*类似 2.表示集合元素 val a = (1 to 10).filter(_%2==0).map(_*2) / ...

  3. Delphi 10.3 Rio + iOS 12.1 SDK 编译错误 "libcharset.1.dylib"

    环境版本: Delphi 10.3 Rio iOS 12.1 SDK Xcode 10.1 (10B61) 错误讯息:[DCC Error] E2597 ld: file not found: /us ...

  4. HTML基础之CSS

    CSS选择器 1.id选择器 2.class选择器 3.标签选择器 4.层级选择器(空格) 5.组合选择器(逗号) 6.属性选择器(中括号) <!DOCTYPE html> <htm ...

  5. 【ORACLE】oracle11g单实例安装

    -- 上传安装包 p13390677_112040_Linux-x86-64_1of7.zip p13390677_112040_Linux-x86-64_2of7.zip -- 解压安装包 unzi ...

  6. JavaScript快速入门-ECMAScript本地对象(Date)

    JavaScript中的Date 对象用于处理日期和时间. var myDate=new Date()  #Date 对象会自动把当前日期和时间保存为其初始值. 一.Date对象的方法 方法 示例 n ...

  7. REST-framework快速构建API--初体验

    一.快速上手 1.环境准备 安装restframework,注册app pip install djangorestframework INSTALLED_APPS = [ 'django.contr ...

  8. 基于Vue手写一个下拉刷新

    当然不乏有很多下拉刷新的插件可以直接使用,但是自定义程度不强,大部分都只能改改文字,很难满足设计师的创意,譬如淘宝和京东首页那种效果,就需要自己花心思倒腾了,最近刚好有这种需求,做完了稍微总结一下,具 ...

  9. 软件测试_测试工具_Loadrunner_IP欺骗

    一.设置IP欺骗的原因: 1.当某个IP的访问过于频繁或者访问量过大时,服务器会拒绝访问请求: 2.某些服务器配置了负载均衡,使用同一个IP不能测出系统的实际性能.Loadrunner中的IP欺骗通过 ...

  10. [翻译]:Artificial Intelligence for games 5.3 STATE MACHINES:状态机

    目录 Chapter 5 Decision Making 5.3 STATE MACHINES:状态机 Chapter 5 Decision Making 5.3 STATE MACHINES:状态机 ...