<p>You are given an array of variable pairs <code>equations</code> and an array of real numbers <code>values</code>, where <code>equations[i] = [A<sub>i</sub>, B<sub>i</sub>]</code> and <code>values[i]</code> represent the equation <code>A<sub>i</sub> / B<sub>i</sub> = values[i]</code>. Each <code>A<sub>i</sub></code> or <code>B<sub>i</sub></code> is a string that represents a single variable.</p>

<p>You are also given some <code>queries</code>, where <code>queries[j] = [C<sub>j</sub>, D<sub>j</sub>]</code> represents the <code>j<sup>th</sup></code> query where you must find the answer for <code>C<sub>j</sub> / D<sub>j</sub> = ?</code>.</p>

<p>Return <em>the answers to all queries</em>. If a single answer cannot be determined, return <code>-1.0</code>.</p>

<p><strong>Note:</strong> The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.</p>

<p><strong>Note:&nbsp;</strong>The variables that do not occur in the list of equations are undefined, so the answer cannot be determined for them.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> equations = [[&quot;a&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;c&quot;]], values = [2.0,3.0], queries = [[&quot;a&quot;,&quot;c&quot;],[&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;e&quot;],[&quot;a&quot;,&quot;a&quot;],[&quot;x&quot;,&quot;x&quot;]]
<strong>Output:</strong> [6.00000,0.50000,-1.00000,1.00000,-1.00000]
<strong>Explanation:</strong> 
Given: <em>a / b = 2.0</em>, <em>b / c = 3.0</em>
queries are: <em>a / c = ?</em>, <em>b / a = ?</em>, <em>a / e = ?</em>, <em>a / a = ?</em>, <em>x / x = ? </em>
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
note: x is undefined =&gt; -1.0</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> equations = [[&quot;a&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;c&quot;],[&quot;bc&quot;,&quot;cd&quot;]], values = [1.5,2.5,5.0], queries = [[&quot;a&quot;,&quot;c&quot;],[&quot;c&quot;,&quot;b&quot;],[&quot;bc&quot;,&quot;cd&quot;],[&quot;cd&quot;,&quot;bc&quot;]]
<strong>Output:</strong> [3.75000,0.40000,5.00000,0.20000]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> equations = [[&quot;a&quot;,&quot;b&quot;]], values = [0.5], queries = [[&quot;a&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;c&quot;],[&quot;x&quot;,&quot;y&quot;]]
<strong>Output:</strong> [0.50000,2.00000,-1.00000,-1.00000]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= equations.length &lt;= 20</code></li>
	<li><code>equations[i].length == 2</code></li>
	<li><code>1 &lt;= A<sub>i</sub>.length, B<sub>i</sub>.length &lt;= 5</code></li>
	<li><code>values.length == equations.length</code></li>
	<li><code>0.0 &lt; values[i] &lt;= 20.0</code></li>
	<li><code>1 &lt;= queries.length &lt;= 20</code></li>
	<li><code>queries[i].length == 2</code></li>
	<li><code>1 &lt;= C<sub>j</sub>.length, D<sub>j</sub>.length &lt;= 5</code></li>
	<li><code>A<sub>i</sub>, B<sub>i</sub>, C<sub>j</sub>, D<sub>j</sub></code> consist of lower case English letters and digits.</li>
</ul>

class Solution {
public:
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
      // Map known variable names to ids
      int id = 0;
      unordered_map<string, int> vars;
      for (const auto &eq : equations) {
        if (vars.emplace(eq[0], id).second) ++id;
        if (vars.emplace(eq[1], id).second) ++id;
      }

      // Setup union-find structure
      // For each variable, we store its parent and a weight for that parent edge
      // The weight represents the fraction 'x / parent' for its immediate parent
      vector<pair<int, double>> groups(id);
      for (int i = 0; i < id; ++i) groups[i] = {i, 1};

      std::function<pair<int, double>(int)> find = [&](int x) -> pair<int, double> {
        auto [group_id, weight] = groups[x];
        if (group_id == x) return {group_id, weight};

        auto [parent_gid, parent_w] = find(group_id);

        // Compress the path. We compress the weights by multiplying them
        // parent_w = group_id / parent_gid
        // weight = x / group_id
        // want: x / parent_gid = weight * parent_w
        groups[x] = {parent_gid, weight * parent_w};
        return {parent_gid, weight * parent_w};
      };

      std::function<void(int, int, double)> unioN = [&](int numerator, int denom, double value) {
        auto [num_gid, num_val] = find(numerator);
        auto [denom_gid, denom_val] = find(denom);
        // The equations must be consistent, so this doesn't give us any new information
        if (num_gid == denom_gid) return;

        // We are given:
        // 1. numerator / denom = value
        // 2. numerator / num_gid = num_val
        // 3. denom / denom_gid = denom_val
        // We want: num_gid / denom_gid
        // num_gid / denom_gid = (num_gid / numerator) * (numerator / denom) * (denom / denom_gid)
        // <=> num_gid / denom_gid = (1 / num_val) * value * denom_val
        groups[num_gid] = {denom_gid, denom_val * value / num_val};
      };

      for (int i = 0, n = equations.size(); i < n; ++i) {
        int numerator = vars[equations[i][0]];
        int denominator = vars[equations[i][1]];
        unioN(numerator, denominator, values[i]);
      }

      vector<double> ans;
      ans.reserve(queries.size());
      for (const auto &query : queries) {
        if (!vars.count(query[0]) || !vars.count(query[1])) {
          ans.push_back(-1);
          continue;
        }

        int numerator = vars[query[0]];
        int denominator = vars[query[1]];
        if (numerator == denominator) {
          ans.push_back(1);
          continue;
        }

        auto [num_gid, num_val] = find(numerator);
        auto [denom_gid, denom_val] = find(denominator);
        if (num_gid != denom_gid) {
          ans.push_back(-1);
          continue;
        }

        // numerator / denominator = (numerator / num_gid) * (num_gid / denominator)
        ans.push_back(num_val / denom_val);
      }
      return ans;
    }
};
10500