HDU2444 The Accomodation of Students (Bipartite Graph Judgment + Hungarian Match)

Topic link

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

Title

Divide n students into two groups so that any two students in the same group know each other. If it can be achieved, output the maximum number of students who know each other, otherwise output'No'.

Ideas

If the grouping method happens to be a bipartite graph, first use the coloring method to determine whether it is a bipartite graph, and if so, perform bipartite matching.

Code

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
vector<int>a[maxn];
bool ch[maxn];
int m,n,u,v,sum,ed[maxn],color[maxn];
bool dfs(int x,int c)
{
    
    
	color[x]=c;
	for(int i=0;i<a[x].size();i++)
	{
    
    
		if(color[a[x][i]]==c)
			return false;
		if(color[a[x][i]]==0&&!dfs(a[x][i],-c))
			return false;
	}
	return true;
}
bool solve()
{
    
    
	for(int i=1;i<=n;i++)
	{
    
    
		if(!color[i])
		{
    
    
			if(!dfs(i,1))
				return false;
		}
	}
	return true;
}
bool find(int x)
{
    
    
	for(int i=0;i<a[x].size();i++)
	{
    
    
		if(!ch[a[x][i]])
		{
    
    
			ch[a[x][i]]=1;
			if(!ed[a[x][i]]||find(ed[a[x][i]]))
			{
    
    
				ed[a[x][i]]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
    
    
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	while(scanf("%d%d",&n,&m)!=EOF)
	{
    
    
		for(int i=0;i<=n;i++)
			a[i].clear();
		memset(ed,0,sizeof ed);
		memset(color,0,sizeof color);
		for(int i=1;i<=m;i++)
		{
    
    
			scanf("%d%d",&u,&v);
			a[u].push_back(v);
			a[v].push_back(u);
		}
		if(!solve())
		{
    
    
			printf("No\n");
			continue;
		}
		sum=0;
		for(int i=1;i<=n;i++)
		{
    
    
			if(color[i]==1)
			{
    
    
				memset(ch,0,sizeof ch);
				if(find(i))
					sum++;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

Tip: Since there are many arrays, you need to pay attention to the definition of each array type, otherwise unpredictable errors will occur. (QAQ)

Guess you like

Origin blog.csdn.net/WTMDNM_/article/details/108628893