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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| class unionFindSet { public: bool exist(const string& s) { return parents_.count(s); }
pair<string, double> find(const string& s) { if (!parents_.count(s)) parents_.emplace(s, pair<string, double>{s, 1.0});
if (parents_[s].first != s) { auto p = find(parents_[s].first); parents_[s].first = p.first; parents_[s].second *= p.second; }
return parents_[s]; }
bool unionTwo(const string& x, const string& y, double val) { const auto& [px, vx] = find(x); const auto& [py, vy] = find(y);
if (px == py) return false;
if (ranks_[px] < ranks_[py]) { parents_[px] = make_pair(py, val * vy / vx); } else if (ranks_[px] > ranks_[py]) parents_[py] = make_pair(px, vx / val / vy); else { parents_[px] = make_pair(py, val * vy / vx); ranks_[py]++; }
return true; }
private: unordered_map<string, pair<string, double>> parents_; unordered_map<string, int> ranks_; };
class Solution { public: vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) { unionFindSet ufs; for (int i = 0; i < equations.size(); ++i) { ufs.unionTwo(equations[i].front(), equations[i].back(), values[i]); }
vector<double> ans; for (const auto& it: queries) { auto a = it.front(); auto b = it.back(); if (!ufs.exist(a) || !ufs.exist(b)) ans.push_back(-1.0); else { const auto& [pa, va] = ufs.find(a); const auto& [pb, vb] = ufs.find(b); if (pa != pb) ans.push_back(-1.0); else { ans.push_back(va / vb); } } }
return ans; } };
|