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. Java泛型:List<?>与List的区别

    为什么说List<?>是type-safe而List不是type-safe的? 1.List<?> compiler看到了你使用了wildcard ?,那么相当于你对compi ...

  2. istio promethus收集不到数据

    1.重新apply istio.yaml kubectl apply -f install/kubernetes/istio.yaml kubectl get metrics.config.istio ...

  3. C# oracle 日期型字段,使用参数传值时,遇到ORA-01810: format code appears twice错误

    C#操作oracle数据库时,发现使用to_date('2014-01-03 18:00:00','yyyy-mm-dd hh:MM:ss')时,会出现ORA-01810: format code a ...

  4. PAT 1001 害死人不偿命的(3n+1)猜想 (15)(C++&JAVA&Python)

    1001 害死人不偿命的(3n+1)猜想 (15)(15 分) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反 ...

  5. linux下查看内存使用情况

    基本内存术语解读 1> free -m 同样是做为缓存,buffers和cache又有啥区别呢? 于是又查了些资料,发现buffers实际应该是叫“缓冲”,其英文解释是:A buffer is ...

  6. fedora25的nfs文件系统搭建

    关于NFS的原理介绍可以参考这篇文章:http://blog.51cto.com/atong/1343950 1> nfs服务端 安装nfs工具包和rpcbind包 dnf install nf ...

  7. Scrapy框架学习笔记

    1.Scrapy简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网 ...

  8. PS快捷键大全,记住这些就够了!

    希望能帮到大家更好的学习.

  9. Git简单操作命令

    Git 1.创建远程分支(git项目已在) git checkout -b cgy git add . git commit -m “add new branch” git push origin c ...

  10. 使用JavaScript实现表现和数据分离

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...