-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate-division.cpp
32 lines (31 loc) · 1.2 KB
/
evaluate-division.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
unordered_map<string,unordered_map<string,double>> graph;
for(int i=0;i<values.size();i++){
string a=equations[i].first;
string b=equations[i].second;
graph[a][b]=values[i];
graph[b][a]=1.0/values[i];
}
vector<double> result(queries.size());
for(int i=0;i<result.size();i++){
set<string> now;
result[i]=dfs(queries[i].first,queries[i].second,1.0,graph,now);
if(result[i]==0.0) result[i]=-1.0;
}
return result;
}
double dfs(string begin,string end,double value,unordered_map<string,unordered_map<string,double>> & graph,set<string>& now){
if(now.count(begin)!=0) return 0.0;
if(graph.count(begin)==0) return 0.0;
if(begin==end) return value;
now.insert(begin);
for(auto e:graph[begin]){
double parResult=dfs(e.first,end,e.second*value,graph,now);
if(parResult!=0.0) return parResult;
}
//now.erase(begin);
return 0.0;
}
};