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 N$ \ge$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 $ \neq$ 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$ \le$N$ \le$36, and indicates the number of T-shirts. Number M, 1$ \le$M$ \le$30, indicates the number of volunteers, with N$ \ge$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

 

 

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class MyTShirtSuitsMe{
private:
    int volunteer;
    int tshirt;
    int match;
    vector<vector<int> > fit;
    vector<bool> visit;
    vector<int> link;
    bool findPath(int);
public:
    void initialize();
    void readCase();
    void computing();
    void outResult();
};

void MyTShirtSuitsMe::initialize(){
    fit.clear();
    link.clear();
    match = 0;
}

void MyTShirtSuitsMe::readCase(){
    cin >> tshirt >> volunteer;
    string str;
    vector<int> cloth;
    for(int i = 0; i < volunteer; i++){
        int n = 2;
        cloth.clear();
        while(n--){
            cin >> str;
            if(str == "XXL"){
                for(int j = 0; j < tshirt / 6; j++){cloth.push_back(j);}
            }
            else if(str == "XL"){
                for(int j = tshirt / 6; j < tshirt / 3; j++){cloth.push_back(j);}
            }
            else if(str == "L"){
                for(int j = tshirt / 3; j < tshirt / 2; j++){cloth.push_back(j);}
            }
            else if(str == "M"){
                for(int j = tshirt / 2; j < tshirt * 2 / 3; j++){cloth.push_back(j);}
            }
            else if(str == "S"){
                for(int j = tshirt * 2 / 3; j < tshirt * 5 / 6; j++){cloth.push_back(j);}
            }
            else if(str == "XS"){
                for(int j = tshirt * 5 / 6; j < tshirt; j++){cloth.push_back(j);}
            }
        }
        fit.push_back(cloth);
    }
    for(int i = 0; i < tshirt; i++){link.push_back(-1);}
}

bool MyTShirtSuitsMe::findPath(int x){
    for(int i = 0; i < fit[x].size(); i++){
        if(!visit[fit[x][i]]){
            visit[fit[x][i]] = true;
            if(link[fit[x][i]] == -1 || findPath(link[fit[x][i]])){
                link[fit[x][i]] = x;
                return true;
            }
        }
    }
    return false;
}

void MyTShirtSuitsMe::computing(){
    for(int i = 0; i < volunteer; i++){
        visit.clear();
        visit.resize(tshirt);
        if(findPath(i)){match++;}
    }
}

void MyTShirtSuitsMe::outResult(){
    if(match == volunteer){cout << "YES" << endl;}
    else{cout << "NO" << endl;}
}

int main(){
    MyTShirtSuitsMe mtsm;
    int ncase;
    cin >> ncase;
    while(ncase--){
        mtsm.initialize();
        mtsm.readCase();
        mtsm.computing();
        mtsm.outResult();
    }
    return 0;
}