poj2446 Chessboard

33 篇文章 0 订阅
9 篇文章 0 订阅

判断是否是完全匹配,和poj3020相似,给格子编号,由于水平不够效率很低,很多大神用的完全匹配,有一点值得学习,就是根据i+j的奇偶性,把点分成两个集合

#include<iostream>
#include<vector>
using namespace std;
bool vis[33*33];
int result[33*33];
bool edge [33*33][33*33];
struct c
{
	int x,y;
	int num;
	c()
	{
		num=0;
	}
};
bool edg[33][33];
bool refresh(int a,int n)
{
	for(int i=1;i!=n;i++)
	{
		if(!vis[i] && edge[a][i])
		{
			vis[i] = true;
			if(result[i]==0 || refresh(result[i],n))
			{
				result[i] = a;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	int n,m,h;
	while(cin>>n>>m>>h)
	{
		c country[33][33];
		int count(1);
		memset(edg,0,sizeof(edg));
		memset(edge,0,sizeof(edge));
		memset(result,0,sizeof(result));
		while(h--)
		{
			int a,b;cin>>a>>b;
			edg[b][a] = true;
		}
		for(int i=1;i!=n+1;i++)
			for(int j=1;j!=m+1;j++)
			{
				if(edg[i][j]==false)
				{
					c node;node.x=j;node.y=i;node.num=count;
					country[i][j].num = count;
					if(i!=1)
						if(country[i-1][j].num!=0)
							edge[count][country[i-1][j].num] = edge[country[i-1][j].num][count] = 1;
					if(j!=1)
						if(country[i][j-1].num!=0)
							edge[count][country[i][j-1].num] = edge[country[i][j-1].num][count] = 1;
					count++;
				}
			}
		/*for(int i=1;i!=count;i++)
		{
			for(int j=1;j!=count;j++)
				cout<<edge[i][j]<<" ";
			cout<<endl;
		}*/
		int ans = 0;
		for(int i=1;i!=count;i++)
		{
			memset(vis,0,sizeof(vis));
			if(refresh(i,count))
				ans++;
		}
		count--;
		if(count==ans)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值