uva - 599 The Forrest for the Trees Solution
The Forrest for the Trees
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<string>
using namespace std;
typedef vector<char> vector_char;
typedef map<char, vector_char> map_vector_char;
typedef set<char> set_char;
typedef map_vector_char::iterator map_vector_char_iterator;
typedef set_char::iterator set_char_iterator;
int main(){
freopen("input.txt", "r", stdin);
int i,test,forest,acorn;
string str;
char ch1,ch2;
map_vector_char mymap;
map_vector_char_iterator mit;
set_char myset;
set_char_iterator sit;
queue<char> q;
cin >> test;
getchar();
while (test--){
mymap.clear();
while (true){
getline(cin, str);
if (str[0] == '*')
break;
ch1 = str[1];
ch2 = str[3];
mit = mymap.find(ch1);
if (mit == mymap.end()){
vector_char vec;
vec.push_back(ch2);
mymap[ch1] = vec;
}
else{
mit->second.push_back(ch2);
}
mit = mymap.find(ch2);
if (mit == mymap.end()){
vector_char vec;
vec.push_back(ch1);
mymap[ch2] = vec;
}
else{
mit->second.push_back(ch1);
}
}
myset.clear();
getline(cin, str);
for (i = 0; i < str.length(); i = i +2){
myset.insert(str[i]);
}
forest = 0;
acorn = 0;
while (!myset.empty()){
sit = myset.begin();
ch1 = *sit;
mit = mymap.find(ch1);
if (mit == mymap.end()){
acorn++;
myset.erase(sit);
}
else{
forest++;
q.push(ch1);
while (!q.empty()){
ch1 = q.front();
q.pop();
sit = myset.find(ch1);
if (sit != myset.end()){
myset.erase(sit);
vector_char vec = mymap[ch1];
for (i = 0; i < vec.size(); i++){
ch1 = vec[i];
sit = myset.find(ch1);
if (sit != myset.end()){
q.push(ch1);
}
}
}
}
}
}
cout << "There are " << forest << " tree(s) and " << acorn << " acorn(s)." << endl;
}
return 0;
}
Wynn Resorts, Ltd. - Mapyro
ReplyDeleteWynn Resorts, Limited is a real estate 여수 출장마사지 developer and developer of luxury 평택 출장안마 hotels and destination 성남 출장마사지 casino 세종특별자치 출장안마 resorts in Las Vegas, Nevada, 수원 출장마사지 United States.