首页 > 其他 > 详细

UVa 11045 My T-shirt suits me

时间:2014-02-22 20:11:02      阅读:380      评论:0      收藏:0      [点我收藏+]
  My T-shirt suits me 

Our friend Victor participates as an instructor in an environmental volunteer program. His boss asked Victor to distribute N T-shirts to M volunteers, one T-shirt each volunteer, where N is multiple of six, and Nbubuko.com,布布扣M. There are the same number of T-shirts of each one of the six available sizes: XXL, XL, L, M , S, and XS. Victor has a little problem because only two sizes of the T-shirts suit each volunteer.

 

 


You must write a program to decide if Victor can distribute T-shirts in such a way that all volunteers get a T-shirt that suit them. If N bubuko.com,布布扣 M, there can be some remaining T-shirts.

 

Input 

The first line of the input contains the number of test cases. For each test case, there is a line with two numbers N and MN is multiple of 6, 1bubuko.com,布布扣Nbubuko.com,布布扣36, and indicates the number of T-shirts. Number M1bubuko.com,布布扣Mbubuko.com,布布扣30, indicates the number of volunteers, with Nbubuko.com,布布扣M. Subsequently, M lines are listed where each line contains, separated by one space, the two sizes that suit each volunteer (XXL, XL, L, M , S, or XS).

 

Output 

For each test case you are to print a line containing YES if there is, at least, one distribution where T-shirts suit all volunteers, or NO, in other case.

 

Sample Input 

 

 
3
18 6
L XL
XL L
XXL XL
S XS
M S
M L
6 4
S XL
L S
L XL
L XL
6 1
L M

 

Sample Output 

 

 
YES
NO
YES

 

 二分图匹配问题,将志愿者和就服匹配起来

直接套用匈牙利算法模板即可

 

bubuko.com,布布扣
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<string>
  5 #include<vector>
  6 #define MAX_V 80
  7 
  8 using namespace std;
  9 
 10 int V;
 11 int n,m;
 12 int match[MAX_V];
 13 bool used[MAX_V];
 14 vector<int> G[MAX_V];
 15 
 16 void add_edge(int u,int v)
 17 {
 18     G[u].push_back(v);
 19     G[v].push_back(u);
 20 }
 21 
 22 bool dfs(int v)
 23 {
 24     used[v]=true;
 25 
 26     for(int i=0;i<G[v].size();i++)
 27     {
 28         int u=G[v][i],w=match[u];
 29         if(w<0||(!used[w]&&dfs(w)))
 30         {
 31             match[v]=u;
 32             match[u]=v;
 33             return true;
 34         }
 35     }
 36 
 37     return false;
 38 }
 39 
 40 int bipartite_matching()
 41 {
 42     int res=0;
 43 
 44     memset(match,-1,sizeof(match));
 45 
 46     for(int v=1;v<=V;v++)
 47     {
 48         if(match[v]<0)
 49         {
 50             memset(used,false,sizeof(used));
 51             if(dfs(v))
 52                 res++;
 53         }
 54     }
 55 
 56     return res;
 57 }
 58 
 59 int main()
 60 {
 61     int kase;
 62 
 63     scanf("%d",&kase);
 64 
 65     while(kase--)
 66     {
 67         scanf("%d %d",&n,&m);
 68 
 69         V=n+m;
 70         for(int i=1;i<=V;i++)
 71             G[i].clear();
 72 
 73         string s1,s2;
 74         for(int i=1;i<=m;i++)
 75         {
 76             cin>>s1>>s2;
 77             if(s1=="XXL"||s2=="XXL")
 78                 for(int j=1;j<=n/6;j++)
 79                     add_edge(j,n+i);
 80             if(s1=="XL"||s2=="XL")
 81                 for(int j=n/6+1;j<=n/3;j++)
 82                     add_edge(j,n+i);
 83             if(s1=="L"||s2=="L")
 84                 for(int j=n/3+1;j<=n/2;j++)
 85                     add_edge(j,n+i);
 86             if(s1=="M"||s2=="M")
 87                 for(int j=n/2+1;j<=n*2/3;j++)
 88                     add_edge(j,n+i);
 89             if(s1=="S"||s2=="S")
 90                 for(int j=n*2/3+1;j<=n*5/6;j++)
 91                     add_edge(j,n+i);
 92             if(s1=="XS"||s2=="XS")
 93                 for(int j=n*5/6+1;j<=n;j++)
 94                     add_edge(j,n+i);
 95         }
 96 
 97         if(bipartite_matching()==m)
 98             puts("YES");
 99         else
100             puts("NO");
101     }
102 
103     return 0;
104 }
[C++]

UVa 11045 My T-shirt suits me

原文:http://www.cnblogs.com/lzj-0218/p/3560576.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!