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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| class Solution { public: vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) { int varCnt = 0; unordered_map<string, int> variables; const int n = equations.size(); for (int i = 0; i < n; ++i) { if (variables.find(equations[i][0]) == variables.end()) variables[equations[i][0]] = varCnt++; if (variables.find(equations[i][1]) == variables.end()) variables[equations[i][1]] = varCnt++; } vector<int> f(varCnt); vector<double> w(varCnt, 1.0); for (int i = 0; i < varCnt; ++i) f[i] = i; for (int i = 0; i < n; ++i) { int leftVar = variables[equations[i][0]], rightVar = variables[equations[i][1]]; merge(f, w, leftVar, rightVar, values[i]); } vector<double> ans; for (const auto& q : queries) { double result = -1.0; if (variables.find(q[0]) != variables.end() && variables.find(q[1]) != variables.end()) { int leftIndex = variables[q[0]], rightIndex = variables[q[1]]; int fl = findf(f, w, leftIndex), fr = findf(f, w, rightIndex); if (fl == fr) result = w[leftIndex] / w[rightIndex]; } ans.push_back(result); } return ans; } private: int findf(vector<int>& f, vector<double>& w, int x) { if (x != f[x]) { int father = findf(f, w, f[x]); w[x] = w[x] * w[f[x]]; f[x] = father; } return f[x]; } void merge(vector<int>& f, vector<double>& w, int x, int y, double val) { int fx = findf(f, w, x); int fy = findf(f, w, y); f[fx] = fy; w[fx] = val * w[y] / w[x]; } };
|