目录
一、题目描述
给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。
另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。
返回 所有问题的答案 。如果存在某个无法确定的答案,则用 -1.0 替代这个答案。
注意:输入总是有效的。你可以假设除法运算中不会出现除数为 0 的情况,且不存在任何矛盾的结果。
示例 1:
输入:equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
输出:[6.00000,0.50000,-1.00000,1.00000,-1.00000]
解释:
条件:a / b = 2.0, b / c = 3.0
问题:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
结果:[6.0, 0.5, -1.0, 1.0, -1.0 ]
示例 2:
输入:equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
输出:[3.75000,0.40000,5.00000,0.20000]
示例 3:
输入:equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
输出:[0.50000,2.00000,-1.00000,-1.00000]
提示:
- 1 <= equations.length <= 20
- equations[i].length == 2
- 1 <= Ai.length, Bi.length <= 5
- values.length == equations.length
- 0.0 < values[i] <= 20.0
- 1 <= queries.length <= 20
- queries[i].length == 2
- 1 <= Cj.length, Dj.length <= 5
- Ai, Bi, Cj, Dj 由小写英文字母与数字组成
二、解题思路
a/b=x,可以把a,b看成图中的节点,x看成是a到b的有向边的权值。所以这是一道关于有向图的问题,而且是与连通块有关。
老方法了,关于图的连通块问题,可以使用三种方法:dfs、BFS、并查集。
官方题解给的是string映射成int来做并查集,这样操作起来,写代码也更方便一些,非常值得学习的手法。
我贴出自己直接使用string类型来做并查集的解题代码,本质上没有区别(重点的查询与合并的权值更新是一样的逻辑),可能看起来会比较直观一些。仅从提交结果来看,效率好像还不错。
重点列出权值更新的操作:
查找:
对于a->b->c->d->e,这种情形的查找,权值的更新方式是:路径上的权值的乘积。比如d->e的权值是1,c->d的权值是2,那么经过路径压缩c->e的权值为1*2=2;这个过程是递归的。
合并:
有两颗树:a->b
c->d;
现在两颗树中间新增一条边b->d,将是:
此时各自找到b和d的父节点,再把a和c两个节点合并。注意a->c->d这条路径与a->b->d两条路径的总权值是一样的。利用这个等价关系可以得到a->c的权值。
三、代码实现
//构建pair类型的哈希函数
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
//father数组
unordered_map<string, string> father;
//string之间对应的weight值
unordered_map<pair<string, string>, double, hash_pair> weight;
string findFather(string s) {
if (s == father[s]) {
return s;
}
string fs = findFather(father[s]);
//更新权值,等于路径上所有的乘积
weight[{fs, s}] = weight[{fs, father[s]}] * weight[{father[s], s}];
father[s] = fs;
return fs;
}
void Union(string s1, string s2, double w) {
string fs1 = findFather(s1);
string fs2 = findFather(s2);
if (fs1 != fs2) {
father[fs2] = fs1;
//设置合并的权值,两颗树合并,两条路径上的乘积相等
//weight[{fs1, fs2}]*weight[{fs2, s2}] = w * weight[{fs1, s1}]
weight[{fs1, fs2}] = w * weight[{fs1, s1}] / weight[{fs2, s2}];
}
}
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
int n = equations.size();
if (n == 0) return {};
//初始化father和weight
for (int i = 0; i < n; i++) {
father[equations[i][0]] = equations[i][0];
father[equations[i][1]] = equations[i][1];
weight[{equations[i][0], equations[i][0]}] = 1.0;
weight[{equations[i][1], equations[i][1]}] = 1.0;
weight[{equations[i][0], equations[i][1]}] = values[i];
}
//合并除数与被除数
for (int i = 0; i < n; i++) {
Union(equations[i][0], equations[i][1], values[i]);
}
int length = queries.size();
vector<double> res;
for (auto& q : queries) {
double result = -1;
if (father.count(q[0]) && father.count(q[1])) {
string f0 = findFather(q[0]), f1 = findFather(q[1]);
if (f0 == f1) {
result = weight[{f0, q[1]}] / weight[{f0, q[0]}];
}
}
res.push_back(result);
}
return res;
}