UVA 11045 My T-shirt suits me【二部图是否全匹配+DFS邻接矩阵实现】
2013-04-21 11:51
399 查看
题目大意:1,西服有六种款式{"XXL", "XL", "L", "M", "S", "XS"},给出西服总数,保证西服总数是衣服款式的整数倍,即每款西服数量相同;
2,每个人可选择两款衣服,但一个人只能挑一件适合自己的衣服;
3,若最后所有人都有适合自己的衣服,输出YES,否则输出NO;
解题策略:1,很明显的二部图最大匹配,只不过要最后检验每个人是否被匹配到;
2,建图时,将每款衣服拆成每件衣服,二部图模型={西服总数,人};
3,练习了一下用邻接矩阵求最大匹配,还有就是调试的过程要细心,我在调试的过程中一直忘记在Main函数里加Initial()函数,结果老不对。。汗。
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; }
相关文章推荐
- uva 11045 - My T-shirt suits me(dfs)
- UVa 11045 - My T-shirt suits me(最大流)
- UVA 11045-My T-shirt suits me(二分图匹配)
- UVa 11045 My T-shirt suits me / 二分图
- UVA 11045 My T-shirt suits me 网络流构图
- uva 11045 My T-shirt suits me
- My T-shirt suits me - UVa 11045 最大流
- UVA 11045 My T-shirt suits me
- UVA 11045 My T-shirt suits me
- UVa 11045 - My T-shirt suits me(最大流)
- UVa 11045 My T-shirt suits me
- UVa 11045 - My T-shirt suits me(最大流)
- uva 11045 My T-shirt suits me
- UVA 11045 - My T-shirt suits me
- UVA 11045 My T-shirt suits me
- UVA - 11045 My T-shirt suits me 网络流
- UVA - 11045 My T-shirt suits me (最大流-二分图匹配)
- UVa 11045 My T-shirt suits me (网络流建图+最大流)
- UVa11045 My T-shirt suits me( 最大流 )
- uva 11045 My T-shirt suits me (二分图匹配 最大流)