UVA 11045 My T-shirt suits me【二部图是否全匹配+DFS邻接矩阵实现】

题目大意:1,西服有六种款式{"XXL", "XL", "L", "M", "S", "XS"},给出西服总数,保证西服总数是衣服款式的整数倍,即每款西服数量相同;

                    2,每个人可选择两款衣服,但一个人只能挑一件适合自己的衣服;

                    3,若最后所有人都有适合自己的衣服,输出YES,否则输出NO;

解题策略:1,很明显的二部图最大匹配,只不过要最后检验每个人是否被匹配到;

                    2,建图时,将每款衣服拆成每件衣服,二部图模型={西服总数,人};

                    3,练习了一下用邻接矩阵求最大匹配,还有就是调试的过程要细心,我在调试的过程中一直忘记在Main函数里加Initial()函数,结果老不对。。汗。


/*
   UVA 11045 My T-shirt suits me
   AC by J_Dark
   ON 2013/4/21
   Time 0.016s
*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <map>
using namespace std;
const int maxn = 40;
map <string, int> Suit;
int suitNum, perNum, NN, g[maxn][maxn];
bool visited[maxn];
int perMatch[maxn], suitMatch[maxn];
/
//建立西服款式与数字一一映射,用Map实现
void Prepare(){
	string suitSize[6]={"XXL", "XL", "L", "M", "S", "XS"};
	for(int i=0; i<6; i++)
	   Suit.insert(pair<string,int>(suitSize[i], i));
}

void Initial(){
	memset(g, 0, sizeof(g));
	memset(visited, false, sizeof(visited));
}

void Input(){
	cin >> suitNum >> perNum;
	NN = suitNum / 6;
	string s1, s2;
	//将每种款式衣服拆成NN件衣服
	for(int i=0; i<perNum; i++){
		cin >> s1 >> s2;
		for(int k=0; k<NN; k++){
		   g[i][Suit[s2]*NN+k] = g[i][Suit[s1]*NN+k] = 1;
		}
	}
}

bool findPath(int u){
	for(int i=0; i<suitNum; i++){
		if(g[u][i] && !visited[i]){
			visited[i] = true;
			if(suitMatch[i]== -1 || findPath(suitMatch[i])){
			   perMatch[u] = i;
			   suitMatch[i] = u;
			   return true;
			}
		}
	}
	return false;
}

void MaxMatch(){
	for(int i=0; i<perNum; i++)   perMatch[i] = -1;
	for(int i=0; i<suitNum; i++)   suitMatch[i] = -1;
	int maxMatch = 0;
	for(int i=0; i<perNum; i++){
		if(perMatch[i] == -1){ //发现未挑衣服者(未盖点)
			   memset(visited, false, sizeof(visited));
			   if(findPath(i)){
				  maxMatch++;
			   }
        }
	}
    int ff=1;
	//寻找是否有未匹配的人
	for(int i=0; i<perNum; i++){
		if(perMatch[i] == -1){
			ff = 0;
			break;
		}
	}
	//cout << maxMatch << " ";
	if(ff) cout << "YES" << endl;
	else cout << "NO" << endl;
}
///
int main(){
	int testCase;
	Prepare();

	while(cin >> testCase)
	{
		while(testCase--)
		{
		    Initial();
			Input();
			MaxMatch();
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值