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

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
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   
#include   
#include   
using namespace std;  

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

//0为白色,1为黑色   
bool BFS(int s, int n) 
{  
    queue 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" <

题解code:

#include 
#include 
#include 
#include 
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){
	queueq;
	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;
}


你可能感兴趣的:(二分图)