UVA 11045 My T-shirt suits me

Source: Internet
Author: User

Original question:
Our friend Victor participates as a instructor in a environmental volunteer program. His bosses asked Victor to distribute n t-shirts to M volunteers, one t-shirt each volunteer, where N was multiple of six, an D n≥m. There is 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 and sizes of the T-shirts suit each volunteer. You must write a program to decide if Victor can distribute T-shirts in such a-the-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 was a line with the 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 is listed where each line contains, separated by one space, the both sizes that suit each volunteer (XXL, XL, L, M, S, or XS).
Output
For the test case you were to print a line containing ' YES ' if there was, at least, one distribution where T-shirts suit al L volunteers, or ' NO ', in other case.
Sample Input
3
18 6
L XL
XL L
XXL XL
S XS
Ms
M L
6 4
S XL
L S
L XL
L XL
6 1
L M
Sample Output
YES
NO
YES


English:
(From Lucky Cat)
Our friend Victor participates in an environmental protection group. His boss asked him to divide N pieces of T-shirt into M-jobs, one for each. Here n must be 6 times, and N >= M. T-shirt has 6 sizes, divided by: XXL, XL, L, M, S, XS. The number of each size T-shirt is the same. Now Victor has a small problem because every volunteer has only 2 T-shirt size to fit him.



You have to write a program to decide whether Victor can give each of them a T-shirt that suits them. If N is not equal to M, there are some T-shirt left.



Input



The first column in the input has an integer representing the following number of tests. The first column of each group of tests has 2 positive integers N, M. N is 6 times, 1 <= N <= 36, representing the number of T-shirt. M, 1 <= m <= 30, representing the number of volunteers, N >= m. Next to the M column, each column has 2 size, which represents the size of the work fit.



Output



Each group of tests outputs one column, and the output is able to give each of them a T-shirt that suits them.


#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;
}


Ideas:



Binary graph problem, can be solved by the Hungarian algorithm, can also be solved with the maximum flow method.
The idea of using the maximum flow method is as shown in the figure
Take the second in the sample data as an example
S is the source point, T is the meeting point, the red circle represents the employee, the hoop represents the clothes, the red circle and the hoop
The source-to-employee capacity is marked as 1, indicating that each employee can only choose one garment, each employee to the same mark as capacity 1, indicating that each person can only choose a dress, clothing to the meeting point of the capacity of each piece of clothing expressed in the total quantity
Finally judging the number of clothes (that is, the maximum flow) and the number of employees is equal can be


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.