class {
public:
double dfs(string from, string to, map<string, map<string, double>> &mp, set<string> & visited, double val)
{
if (mp[from].count(to)) {
return mp[from][to] * val;
}
for (pair<string, double>p : mp[from]) {
string str = p.first;
if (visited.count(str) == 0) {
visited.insert(str);
double cur = dfs(str, to, mp, visited, p.second * val);
if (cur != -1.0) return cur;
}
}
return -1.0;
}
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
vector<double> result;
map<string, map<string, double>> mp;
int i = 0;
for (pair<string, string> p : equations) {
mp[p.first][p.second] = values[i];
mp[p.second][p.first] = 1.0 / values[i];
i++;
}
for (pair<string, string> p : queries) {
if (mp.count(p.first) && mp.count(p.second)) {
if (p.first == p.second) {
result.push_back(1.0);
continue;
}
else {
set<string> visited;
result.push_back(dfs(p.first, p.second, mp, visited, 1.0));
}
}
else {
result.push_back(-1.0);
}
}
return result;
}
};