The Accomodation of Students HDU - 2444(最大二分匹配+染色法判断二分图)


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

思路:首先利用染色法判断一下这个图是否是一个二分图,然后找最大二分匹配,找的过程中为了简单化,把两边均看成有n个点,即正反都记录,求出最终结果,然后用找到的匹配数除以2 就是正确答案

其中染色法:

怎么判定一个图是否为二分图
从其中一个定点开始,将跟它邻接的点染成与其不同的颜色,最后如果邻接的点有相同颜色,则说明不是二分图,每次用bfs遍历即可。

染色法模板:

#include <queue>  
#include <cstring>  
#include <iostream>  
using namespace std;  

const int N = 999;  
int col[N], Map[N][N];  

//0为白色,1为黑色   
bool BFS(int s, int n) 
{  
    queue<int> p;  
    p.push(s);  
    col[s] = 1;  //将搜索起始点涂成黑色
    while(!p.empty()) 
    {  
        int from = p.front();  
        p.pop();  
        for(int i = 1; i <= n; i++) 
        {  
            if(Map[from][i] && col[i] == -1) //如果从from到i的边存在(为邻接点) && i点未着色
            {  
                p.push(i);          //将i点加入队列
                col[i] = !col[from];//将i点染成不同的颜色   
            }  
            if(Map[from][i] && col[from] == col[i])//如果从from到i的边存在(为邻接点) && i点和from点这一对邻接点颜色相同,则不是二分图   
                return false;  
        }  
    }  
    return true;  //搜索完s点和所有点的关系,并将邻接点着色,且邻接点未发现相同色则返回true     
}  

int main() 
{  
    int n, m, a, b;  
    memset(col, -1, sizeof(col));  
    cin >> n >> m;  //n 为有多少点,m为有多少边  
    for(int i = 0; i < m; i++) 
    {  
        cin >> a >> b;  
        Map[a][b] = Map[b][a] = 1;   
    }  
    bool flag = false;  
    for(i = 1; i <= n; i++)  //遍历并搜索各个连通分支
    {    
        if(col[i] == -1 && !BFS(i, n)) //每次找没有着色的点进行判断,如果从它开始BFS发现相同色邻接点则不是二分图
        {   
            flag = true;  
            break;    
        }  
    }
    if(flag)  
        cout << "NO" <<endl;      
    else  
        cout << "YES" <<endl;  
    return 0;  
}  

题解code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int mp[1000][1000];//记录关系的数组 
int match[1000];//最大二分匹配数组 
int vis[1000];//标记数组 
int color[1000];//染色数组
int n,m;
int Judge();//判断是否是二分图;
int bfs(int u);//判断二分图的广搜函数
int findMax();//找最大二分匹配
int dfs(int u);//找最大二分匹配的深搜函数 
int main(){
	while(cin >> n >> m){
		int i;
		memset(mp,0,sizeof(mp));
		for(i = 0; i < m; i++){
			int u,v;
			cin >> u >> v;
			mp[u][v] = mp[v][u] = 1;
		}
		int flag = Judge();
		if(!flag){
			cout << "No" << endl;
			continue;
		}
		cout << findMax()/2 << endl;
	}
	return 0;
}
int Judge(){
	int i;
    memset(color,0,sizeof(color));
	for(i = 1; i <= n; i++){
		if(color[i] == 0 && !bfs(i)){
			return 0;
		}
	}
	return 1;
}
int bfs(int u){
	queue<int>q;
	q.push(u);
	color[u] = 1;//给起点上色
	while(!q.empty()){
		int f = q.front();
		q.pop();
		int i;
		for(i = 1; i <= n; i++){
			if(mp[f][i]){
				if(color[i] == color[f])
				  return 0;
				else if(color[i] == 0){
					color[i] = -color[f];
					q.push(i);
				}
			}
		}
	}
	return 1; 
}
int findMax(){
	int i;
	int cnt = 0;
	memset(match,0,sizeof(match));
	for(i = 1; i <= n; i++){
		memset(vis,0,sizeof(vis));
		if(dfs(i))
		  cnt++;
	}
	return cnt;
}
int dfs(int u){
	int i;
	for(i = 1; i <= n; i++){
		if(mp[u][i] && !vis[i]){
			vis[i] = 1;
			if(!match[i] || dfs(match[i])){
				match[i] = u;
				return 1;
			}
		}
	}
	return 0;
}


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值