題意:給志願者發T恤,總共有6個號,t恤的總數是6的倍數,每個號的t恤數相同,每個志願者都有兩個適合的號,給出t恤的總數,志願者的總數,每個志願者適合的號,算出是否能夠給每個志願者發到合適的衣服。
分析:思路與uva10092The Problem with the Problemsetter完全相同,套用這題的模板修改下,經過前面的辛苦後,這次一遍AC。
代碼:
//uva11045 My T-Shirt Suits Me
//AC By Warteac
//2013-4-21
//Runtime:0.016s
#include<iostream>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std;
const string s[6] = {"XXL","XL","L","M","S","XS"};
class MyTShirtSuitsMe{
private:
vector < vector<int> > selectShirt;//鄰接表
int numberOfShirts;//18
int numberOfVolunteers;//6
int numOfMatch;
private:
vector <int> link;
vector <bool> visited;
bool dfs(int u);
public:
void initial();
void readCase();
void computing();
void outResult();
};
void MyTShirtSuitsMe::initial(){
selectShirt.clear();
numberOfShirts = 0;
numberOfVolunteers = 0;
numOfMatch = 0;
link.clear();
visited.clear();
}
void MyTShirtSuitsMe::readCase(){
cin >> numberOfShirts >> numberOfVolunteers;
int n = numberOfVolunteers,index1,index2;
int num = numberOfShirts/6;
string s1,s2;
vector <int> temp;
while(n--){
cin >> s1 >> s2;
temp.clear();
for(int i = 0; i < 6; i++){
if(s[i] == s1) index1 = i;
if(s[i] == s2) index2 = i;
}
for(int j = num*index1; j < num*(index1+1); j++)
temp.push_back(j);
for(int j = num*index2; j < num*(index2+1); j++)
temp.push_back(j);
selectShirt.push_back(temp);
}
}
bool MyTShirtSuitsMe::dfs(int u){
for(int i = 0; i < selectShirt[u].size(); i++){
int c = selectShirt[u][i];
if(!visited[c]){
visited[c] = true;
if( (link[c] == -1 ) || dfs(link[c])==true ){
link[c] = u;
return true;
}
}
}
return false;
}
void MyTShirtSuitsMe::computing(){//hungary
for(int i = 0; i < numberOfShirts; i++){//initial link
link.push_back(-1);
}
for(int i = 0; i < selectShirt.size(); i++){
visited.clear();
visited.resize(numberOfShirts);
if(dfs(i)) numOfMatch++;
}
}
void MyTShirtSuitsMe::outResult(){
if(numOfMatch == numberOfVolunteers){
cout << "YES" <<endl;
}else{
cout <<"NO" << endl;
}
}
int main(){
MyTShirtSuitsMe t;
int n;
cin >> n;
while(n--){
t.initial();
t.readCase();
t.computing();
t.outResult();
}
}