博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2444(染色法判断二分图+最大匹配)
阅读量:5274 次
发布时间:2019-06-14

本文共 2844 字,大约阅读时间需要 9 分钟。

The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5225    Accepted Submission(s): 2374

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
 
题意:给出 n 个学生,将这些学生互不认识的人分成一组,剩下的人分成另一组,然后在这两组中互相认识的人配对,问最多能够配多少对?
题解:先用染色法对无向图进行判断是否为二分图,不是的话直接输出No,是的话进行最大匹配。网上有很多解法其实是有问题的,题目并没有说是强连通图,都是从1开始判断,但是这组数据就是过不了的,所以我们要对所有点进行判断。
无向图G为二分图的充分必要条件是,G至少有两个顶点,且其所有回路的长度均为偶数。
4 3
2 3
3 4
4 2
ans:No
#include
#include
#include
#include
#include
#include
using namespace std;const int N = 205;int n,m;int graph[N][N],mp[N][N];int linker[N];bool vis[N];int color[N]; ///染色数组bool dfs(int u){ for(int v=1;v<=n;v++){ if(graph[u][v]&&!vis[v]){ vis[v] = true; if(linker[v]==-1||dfs(linker[v])){ linker[v] = u; return true; } } } return false;}bool bfs(int s){ queue
q; color[s] = 0; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); for(int i=1;i<=n;i++){ if(graph[u][i]){ if(color[i]==-1){ ///未染色 color[i] = !color[u]; q.push(i); }else{ if(color[i]==color[u]) return false; } } } } return true;}int main(){ while(scanf("%d%d",&n,&m)!=EOF){ memset(graph,0,sizeof(graph)); memset(color,-1,sizeof(color)); for(int i=1;i<=m;i++){ int u,v; scanf("%d%d",&u,&v); graph[u][v] = 1; graph[v][u] = 1; } bool flag = false; for(int i=1;i<=n;i++){ if(color[i]!=-1) continue; ///已染色 if(!bfs(i)) { flag = true; break; } } if(flag){ printf("No\n"); continue; } memset(linker,-1,sizeof(linker)); int res = 0; for(int i=1;i<=n;i++){ memset(vis,false,sizeof(vis)); if(dfs(i)){ res++; } } printf("%d\n",res/2); } return 0;}

 

转载于:https://www.cnblogs.com/liyinggang/p/5717226.html

你可能感兴趣的文章
weevely-------linux中的菜刀(转载)
查看>>
Optimize Slow VBA Code
查看>>
mysql使用常见问题
查看>>
Porter Stemming Algorithm
查看>>
php foreach循环中的变量
查看>>
elk-logstash时区问题
查看>>
C#应用视频教程3.1 USB工业相机测试
查看>>
实验一 绘制金刚石图案
查看>>
白话SpringCloud | 第五章:服务容错保护(Hystrix)
查看>>
fabricjs 高级篇(自定义类型)
查看>>
jQuery之end()和pushStack()
查看>>
springboot入门_shiro
查看>>
Bootstrap--响应式导航条布局
查看>>
【好程序员笔记分享】——下拉刷新和上拉加载更多
查看>>
多线程,多进程,协程
查看>>
Hacker News与Reddit的算法比较
查看>>
Learning Python 009 dict(字典)和 set
查看>>
JavaScript中随着鼠标拖拽而移动的块
查看>>
mysql-5.7.21-winx64.zip 下载安装
查看>>
Creating a Custom Login Page for SharePoint 2010
查看>>