UVa 11045 My T-shirt suits me

第二道UVA的题目,1A,嘻嘻。

 

类似之前做的那个衣服分配问题,不过这个比那个还简单了。每个人只有两种衣服适合,而且每种衣服的数量是相等的。

 

建图很随意哈,把每种衣服拆成一件一件的,然后跟队员连线求最大二分匹配即可。

 

#include <stdio.h> #include <stdlib.h> #include <iostream> #define MAX 650 using namespace std; int map[MAX][MAX]; int used[MAX],mat[MAX]; char Size[7][5] = {"","XXL","XL","L", "M" , "S","XS"}; int Match(char *a) { int i; for(i=1; i<=6; i++) if( strcmp(a,Size[i]) == 0 ) return i; } void BuildMap(int pos,int x,int arr) { int i; for(i=(pos-1)*arr+1; i<=pos*arr; i++) map[x][i] = 1; } int Augment(int x,int n) { int i; for(i=1; i<=n; i++) if( !used[i] && map[x][i] ) { used[i] = 1; if( !mat[i] || Augment(mat[i],n) ) { mat[i] = x; return 1; } } return 0; } int Hungary(int n,int m) { int i,sum = 0; memset(mat,0,sizeof(mat)); for(i=1; i<=n; i++) { memset(used,0,sizeof(used)); if( Augment(i,m)) sum++; } return sum; } int main() { int ncases,i; int n,m,arr; char T[5]; scanf("%d",&ncases); while( ncases-- ) { memset(map,0,sizeof(map)); scanf("%d%d",&m,&n); arr = m/6; for(i=1; i<=n; i++) { scanf("%s",T); BuildMap(Match(T),i,arr); scanf("%s",T); BuildMap(Match(T),i,arr); } int ans = Hungary(n,m); if( ans != n ) printf("NO/n"); else printf("YES/n"); } return 0; }  

你可能感兴趣的:(UVa 11045 My T-shirt suits me)