链接:https://leetcode-cn.com/problems/evaluate-division/
给你一个变量对数组 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的边,边权为2.0,再反向构建一条边B->A,边权为0.5。而实际上就是要求A->C的路径上的权值乘积。那么整个题目就很简单了,我们可以直接针对每个源点做一次DFS,再查询结果。亦可针对需要的结果进行DFS,在DFS的过程中计算result[i][j]。剩下要做的就是把题目给的字符串映射成数,方便操作。
class Solution {
public:
struct Son{
int v;
double value;
Son(){
}
Son(int v,double value) {
this->v = v;
this->value = value;
}
};
unordered_map<string,int> reflect; //字符串映射为数字便于操作
vector<Son> Vec[50];
double MulResult[50][50];
bool visited[50];
void DFS(int S,int father,double pre){
Son son;
int n = Vec[father].size(), i, v;
double value;
for(i = 0; i < n; ++ i) {
son = Vec[father][i];
v = son.v;
value = son.value;
if( !visited[v] ){
MulResult[S][v] = pre * value;
visited[v] = true;
DFS(S, v, pre * value);
}
}
}
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
//图问题
vector<double> ans;
double res;
int n = equations.size(), i, cnt = 0, p, q;
string u, v;
for(i = 0; i < n; ++ i){
u = equations[i][0];
if(reflect.find(u) == reflect.end() ){
reflect[u] = ++ cnt;
MulResult[cnt][cnt] = 1;
}
v = equations[i][1];
if(reflect.find(v) == reflect.end() ){
reflect[v] = ++ cnt;
MulResult[cnt][cnt] = 1;
}
p = reflect[u];
q = reflect[v];
Vec[p].push_back(Son(q, values[i]));
Vec[q].push_back(Son(p, 1.0 / values[i]));
}
for(i = 1; i <= cnt; ++ i){
if( !visited[i] ) {
visited[i] = i;
DFS(i, i, 1);
}
memset(visited, 0, sizeof(visited));
}
// 你可以假设除法运算中不会出现除数为 0 的情况,且不存在任何矛盾的结果。
n = queries.size();
for(i = 0; i < n; ++ i){
if( reflect.find(queries[i][0]) == reflect.end() || reflect.find(queries[i][1]) == reflect.end() ){ //没有纳入图的点
ans.push_back(-1);
}
else{
p = reflect[queries[i][0]];
q = reflect[queries[i][1]];
if( MulResult[p][q] == 0.0 ) ans.push_back(-1);
else ans.push_back(MulResult[p][q]);
}
}
return ans;
}
};
再谈优化:实际上本题最好的解法是使用带权并查集,需要注意路径压缩时的权值更新问题(这个详见并查集与路径压缩)。最需要处理的就是当两个集合中的元素合并时遇到的问题:
如上图表示的关系为和,现在我们给出关系:
注意,实际上并查集我们不会这样建立的,因为并查集的合并是根节点之间的合并,这里只是个示意图,现在我们要建立A->C之间的关系,事实上,无论是走B->D->C还是B->A->C,B和C的比例关系是确定的,因此我们可以直接计算出,合并后的并查集如下图:
class Solution {
public:
unordered_map<string,int> reflect; //字符串映射为数字便于操作
double EdgeValue[50];
int Father[50];
int Find(int x){
if( x != Father[x] ){
int originFather = Father[x];
Father[x] = Find(Father[x]);
EdgeValue[x] *= EdgeValue[originFather];
}
return Father[x];
}
void Union(int A,int B,double value){
int Fa = Find(A), Fb = Find(B);
if(Fa != Fb){
Father[Fa] = Fb;
// 由于我们是先查 后合并 因此这里的EdgeValue[B]表示的是B和所在集合的根的比例关系
EdgeValue[Fa] = EdgeValue[B] * value / EdgeValue[A];
}
}
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
//图问题
vector<double> ans;
double res;
int n = equations.size(), i, cnt = 0, p, q, Fp, Fq;
string u, v;
for(i = 0; i < n; ++ i){
u = equations[i][0];
if(reflect.find(u) == reflect.end() ){
reflect[u] = ++ cnt;
Father[cnt] = cnt;
EdgeValue[cnt] = 1;
}
v = equations[i][1];
if(reflect.find(v) == reflect.end() ){
reflect[v] = ++ cnt;
Father[cnt] = cnt;
EdgeValue[cnt] = 1;
}
Union(reflect[u], reflect[v], values[i]);
}
// 你可以假设除法运算中不会出现除数为 0 的情况,且不存在任何矛盾的结果。
n = queries.size();
for(i = 0; i < n; ++ i){
if( reflect.find(queries[i][0]) == reflect.end() || reflect.find(queries[i][1]) == reflect.end() ){ //没有纳入图的点
ans.push_back(-1);
}
else{
p = reflect[queries[i][0]];
Fp = Find(p);
q = reflect[queries[i][1]];
Fq = Find(q);
if( Fp != Fq ) ans.push_back(-1); //不在同一个集合
else ans.push_back(EdgeValue[p] / EdgeValue[q]);
}
}
return ans;
}
}