uva 11045My T-shirt suits me

来源:互联网 发布:变化怎样发生 知乎 编辑:程序博客网 时间:2024/06/05 20:32

原题:
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 N ≥ 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 ̸= 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 M. N is multiple of 6, 1 ≤ N ≤ 36, and indicates the number of T-shirts. Number M, 1 ≤ M ≤ 30, indicates the number of volunteers, with N ≥ 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

中文:
(来自lucky 猫)
我們的朋友Victor參加一個環保團體。它的老闆要他把 N 件 T-shirt 分給 M 個義工,每人一件。在這裡 N 一定是 6 的倍數,且 N >= M。T-shirt 有6種 size, 分別是:XXL, XL, L, M, S, XS。每種 size T-shirt 的數量都一樣。現在 Victor 有一個小問題,因為每個義工都只有2種 T-shirt 的 size 適合他。

你必須寫一個程式來決定是否 Victor 可以發給每個義工一件適合他們的 T-shirt。假如 N 不等於 M,那可以有一些 T-shirt 剩下。

Input

輸入的第一列有一個整數代表以下有幾組測試資料。每組測試資料的第一列有2個正整數 N, M。N 是 6 的倍數,1 <= N <= 36, 代表 T-shirt 的數目。M ,1 <= M <= 30,代表義工的數目,N >= M。接下來的 M 列,每列有2個 size,分別代表各義工適合的 size。

Output

每組測試資料輸出一列,輸出能否發給每個義工一件適合他們的 T-shirt。

#include <bits/stdc++.h>using namespace std;const int maxn=100;const int inf=100000;int n,m;struct Edge{    int from,to,cap,flow;    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f) {}};struct EdmondsKarp{    int n,m;    vector<Edge> edges;    vector<int> G[maxn];    int a[maxn];    int p[maxn];    void init(int n)    {        for(int i=0;i<=n;i++)            G[i].clear();        edges.clear();    }    void AddEdge(int from,int to,int cap)    {        edges.push_back(Edge(from,to,cap,0));        edges.push_back(Edge(to,from,0,0));        m=edges.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    int Maxflow(int s,int t)    {        int flow=0;        while(true)        {            memset(a,0,sizeof(a));            queue<int> Q;            Q.push(s);            a[s]=INT_MAX;            while(!Q.empty())            {                int x=Q.front();                Q.pop();                for(int i=0;i<G[x].size();i++)                {                    Edge& e = edges[G[x][i]];                    if(!a[e.to]&&e.cap>e.flow)                    {                        p[e.to]=G[x][i];                        a[e.to]=min(a[x],e.cap-e.flow);                        Q.push(e.to);                    }                }                if(a[t])                    break;            }            if(!a[t])                break;            for(int u=t;u!=s;u=edges[p[u]].from)            {                edges[p[u]].flow+=a[t];                edges[p[u]^1].flow-=a[t];            }            flow+=a[t];        }        return flow;    }};int get_node(string s){    if(s=="L")        return 31;    if(s=="M")        return 32;    if(s=="S")        return 33;    if(s=="XS")        return 34;    if(s=="XL")        return 35;    if(s=="XXL")        return 36;}EdmondsKarp EK;int main(){    ios::sync_with_stdio(false);    int t;    cin>>t;    while(t--)    {        EK.init(50);        cin>>n>>m;        for(int i=1;i<=6;i++)            EK.AddEdge(30+i,37,n/6);        for(int i=1;i<=m;i++)        {            string s1,s2;            cin>>s1>>s2;            int n1=get_node(s1);            int n2=get_node(s2);            EK.AddEdge(i,n1,1);            EK.AddEdge(i,n2,1);            EK.AddEdge(0,i,1);        }        int ans=EK.Maxflow(0,37);//        cout<<ans<<endl;        if(ans==m)            cout<<"YES"<<endl;        else            cout<<"NO"<<endl;    }    return 0;}

思路:

二分图问题,可以用匈牙利算法解决,也可以用最大流方法解决。
使用最大流方法的思路如图所示
以样例数据中的第二个为例子
S是源点,T是汇点,红圈代表员工,篮圈代表衣服,红圈和篮圈
源点到员工的容量标记为1,表示每个员工只能选一个衣服,每个员工到衣服之间同样标记为容量1,表示每人只能选一件衣服,衣服到汇点的容量用每件衣服的总数量表示
最后判断衣服的数量(也就是得到的最大流)和员工的数量是否相等即可
这里写图片描述

0 0