hdu2444 二分图匹配(判断二分图)

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. 

题意就是给你点的个数和一组边,判断这个图是否为二分图,如果是二分图求出最大匹配,不是输出No

建出图之后直接匈牙利就ok,所以题目仅仅难在存点和判断二分图上。

网上答案说什么染色我没管,我就是用奇数代表左二分图偶数代表右二分图,每次输入边的时候判断一下,如果有一个点已经被。。。等下 我是不是自己考虑的方法和染色一模一样。。。不管了,如果有一个点已经被数标记了,那另一个边一定是和它不同奇偶的数,如果一边连着同奇偶性的两点,标记输出No,然后邻接表建图扫一边n,找出奇数标记的进行匈牙利算法,ok

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int n,m;
int lx,ly;
int a[1005];
int girl[1005];//男女搭配
vector<int> mp[1005];
bool vis[1005];
int dfs(int x)
{
	for(int i=0;i<mp[x].size();i++)
	{
		int t=mp[x][i];
		if(!vis[t])
		{
			vis[t]=1;
			if(girl[t]==-1||dfs(girl[t]))
			{
				girl[t]=x;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int x,y;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		bool flag=0;
		memset(a,0,sizeof(a));
		memset(girl,-1,sizeof(girl));
		for(int i=0;i<=max(lx,ly);i++) //尽量把邻接表清空,第一次就wa在这了,哼
		{
			mp[i].clear();
		}
		lx=1,ly=2;
		for(int i=0;i<m;i++)
		{
			scanf("%d %d",&x,&y);
			if(a[x]==0&&a[y]==0)
			{
				a[x]=lx;
				a[y]=ly;
				lx+=2;
				ly+=2;
			}
			else if(a[x]==0)
			{
				if(a[y]&1)
				{
					a[x]=ly;
					ly+=2;
				}
				else
				{
					a[x]=lx;
					lx+=2;
				}
			}
			else if(a[y]==0)
			{
				if(a[x]&1)
				{
					a[y]=ly;
					ly+=2;
				}
				else
				{
					a[y]=lx;
					lx+=2;
				}
			}
			else
			{
				//printf("fuck%d %d",x,y); 
				if((a[x]&1)==(a[y]&1)) //极其诡异的优先级
				{
					flag=1;
					continue;
				}
			}
			mp[x].push_back(y);
			mp[y].push_back(x);
		}
		/*for(int i=1;i<=n;i++)
			printf("fu%d ",a[i]);
			printf("\n");*/
		if(flag)
			printf("No\n");
		else
		{
			int ans=0;
			for(int i=1;i<=n;i++) //扫出奇数点
			{
			memset(vis,0,sizeof(vis));
			if(a[i]&1)
			ans+=dfs(i);
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值