poj2446 Chessboard

判断是否是完全匹配,和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;
}


你可能感兴趣的:(c,struct)