1176 - Getting a T-shirt
![]() ![]() | PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
In programming contests, a common problem for the contestants is to get a suited T-shirt. Sometimes people gets too long or too short T-shirts. So, this time I have planned to ask the authority to manage the T-shirts such that everyone gets a suitable one. From my past experience, it's known that there are 6 available sizes of T-shirts and they are XXL, XL, L, M, S, and XS. And exactly two sizes of the T-shirts suit a person.
Now, for a contest there are T-shirts of N colors and M contestants. And for each color, all the 6 sizes are available. So, there are 6*N T-shirts. And you are given the suitable sizes for each contestant. You have to distribute the T-shirts to the contestants such that everyone gets a suitable size. Only size matters, color is not an issue. Now you have to decide whether it's possible or not.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
The first line of each test case contains two integers N and M, separated by spaces, with 1 ≤ N, M ≤ 50. Each of the next M lines will contain two sizes as described earlier.
Output
For each case, print the case number and "YES" or "NO" depending on whether it's possible to distribute the T-shirts or not.
Sample Input | Output for Sample Input |
3 3 6 L XL XL L XXL XL S XS M S M L 1 4 S XL L S L XL L XL 1 1 L M | Case 1: YES Case 2: NO Case 3: YES |
题目大意:有m个人,衣服有n种颜色,(感觉这个地方有点绕,其实是有6个尺码,每个尺码有n种颜色的衣服,而题目又说了竞赛者不关心衣服颜色,所以意思就是每个尺码有n件衣服),给出每个参赛者想要的两种尺码,问能不能满足所有参赛者的要求。
考虑网络流,源点与每个人连一条容量为1的边,每个人与他想要的尺码连一条容量为1的边,这就限制了每个人只能选一件衣服的条件,对于每个尺码,与汇点连一条容量为n的边,表示每个尺码有n件跑一遍最大流,只要最后跑出的答案等于参赛者的人数就是满足,否则不满足
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
const int maxn=1010;
const int maxm=10010;
const int inf=0x3f3f3f3f;
struct Node
{
int to;
int capa;
int next;
}edge[maxm];
int source,sink;
int cnt;
int head[maxn];
bool vis[maxn];
int num[maxn];
int dep[maxn];
map<string,int> ma;
void init()
{
memset(head,-1,sizeof(head));
ma["XS"]=1;
ma["S"]=2;
ma["M"]=3;
ma["L"]=4;
ma["XL"]=5;
ma["XXL"]=6;
cnt=0;
return;
}
void add(int u,int v,int capa)
{
edge[cnt].to=v;
edge[cnt].capa=capa;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].capa=0;
edge[cnt].next=head[v];
head[v]=cnt++;
return;
}
bool bfs()
{
queue<int> que;
que.push(source);
memset(dep,-1,sizeof(dep));
dep[source]=0;
while(!que.empty())
{
int node=que.front();
que.pop();
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].capa>0&&dep[v]==-1)
{
dep[v]=dep[node]+1;
if(v==sink) return true;
que.push(v);
}
}
}
return dep[sink]!=-1;
}
int dfs(int node,int minn)
{
if(node==sink||minn==0)
{
return minn;
}
int r=0;
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(dep[v]==dep[node]+1&&edge[i].capa>0)
{
int tmp=dfs(v,min(edge[i].capa,minn));
if(tmp>0)
{
edge[i].capa-=tmp;
edge[i^1].capa+=tmp;
r+=tmp;
minn-=tmp;
if(!minn) break;
}
}
}
if(!r) dep[node]=-1;
return r;
}
int dinic()
{
int maxflow=0;
while(bfs())
{
maxflow+=dfs(source,inf);
}
return maxflow;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int test;
scanf("%d",&test);
for(int cas=1;cas<=test;cas++)
{
init();
int n,m;
scanf("%d%d",&n,&m);
source=0;
sink=m+10;
for(int i=1;i<=m;i++)
{
string a,b;
cin>>a>>b;
add(source,i,1);
add(i,ma[a]+m,1);
add(i,ma[b]+m,1);
}
for(int i=1;i<=6;i++)
{
add(m+i,sink,n);
}
printf("Case %d: %s\n",cas,dinic()==m?"YES":"NO");
}
return 0;
}