The Accomodation of Students

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

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   |   We have carefully selected several similar problems for you:  2438 2443 2442 2441 2440 

————————————————————————————————————

题目的意思是给出n组朋友关系,问能否分成2组组内不是朋友,如果可以将朋友两两分组最多分几组

思路:首先用染色法判断是否是二分图,然后最大二分图匹配

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
const int MAXN=1005;
int uN,vN,n; //u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int color[MAXN]; bool dfs(int u)
{
int v;
for(v=1; v<=vN; v++)
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
return false;
} int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=1; u<=uN; u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
} bool bfs(int x)
{
queue<int>q;
q.push(x);
color[x]=1;
while(!q.empty())
{
int f=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(g[f][i]==1)
{
if(color[i]==color[f])
return 0;
else if(color[i]==0)
{
color[i]=-color[f];
q.push(i);
}
}
}
}
return 1; } int main()
{
int m,x,y;
while(~scanf("%d%d",&n,&m))
{
int flag=0;
memset(g,0,sizeof g);
for(int i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
g[x][y]=g[y][x]=1;
}
memset(color,0,sizeof color);
for(int i=1;i<=n;i++)
{
if(color[i]==0)
{
if(!bfs(i))
{
flag=1;
break;
}
}
}
if(flag==1)
{
printf("No\n");
continue;
}
uN=vN=n;
printf("%d\n",hungary()/2);
}
return 0;
}

  

HDU2444 The Accomodation of Students的更多相关文章

  1. HDU2444 The Accomodation of Students —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...

  2. HDU2444 The Accomodation of Students【匈牙利算法】

    题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...

  3. hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)

    //推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...

  4. HDU2444 The Accomodation of Students(二分图最大匹配)

    有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...

  5. HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)

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

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

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

  7. HDOJ 2444 The Accomodation of Students

    染色判读二分图+Hungary匹配 The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limi ...

  8. HD2444The Accomodation of Students(并查集判断二分图+匹配)

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

  9. The Accomodation of Students

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

随机推荐

  1. c++ 备忘

    一.类型转换#include <sstream>stringstream ss;ss<<reverse(s1)<<'\t'<<reverse(s2);s ...

  2. PAT 1042 字符统计(20)(思路)

    1042 字符统计(20)(20 分) 请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过1000的字符串.字符串由ASCII码表中任意可见字符及空格 ...

  3. cesium 中地图发生了平移,放缩,旋转等动作所要执行的动作

    1.在canvas上得到鼠标点击的是那个键 <html><head><title>js判断鼠标左.中.右键哪个被点击-柯乐义</title><sc ...

  4. linux命令行下执行循环动作

    在当前子目录下分别创建x86_64 for dir in `ls `;do (cd $dir;mkdir x86_64);done

  5. 几个H5炫酷特效

    那H5里有哪些高级动效了?小编仔细体验了国内不少几个优秀H5页面作品,整理出下面几个H5页面特效.我们的H5作品如果能用上其中一两个,相信能增色不少! 1.粒子特效 —>>一键爆炸 模拟现 ...

  6. BZOJ1855 股票交易 单调队列优化 DP

    描述 某位蒟佬要买股票, 他神奇地能够预测接下来 T 天的 每天的股票购买价格 ap, 股票出售价格 bp, 以及某日购买股票的上限 as,  某日出售股票上限 bs, 并且每次股票交 ♂ 易 ( 购 ...

  7. Python之路(第二篇):Python基本数据类型字符串(一)

    一.基础 1.编码 UTF-8:中文占3个字节 GBK:中文占2个字节 Unicode.UTF-8.GBK三者关系 ascii码是只能表示英文字符,用8个字节表示英文,unicode是统一码,世界通用 ...

  8. AUC和ROC

    https://www.cnblogs.com/gatherstars/p/6084696.html

  9. Sliding Window Maximum LT239

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  10. Asterisk的type类型和身份认证

    Asterisk的type类型和身份认证 转载:http://zeevli.blog.163.com/blog/static/119591610201111745012380/ 在Asterisk中对 ...