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;
}


發佈了50 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章