当前位置:网站首页>力扣399【除法求值】【并查集】
力扣399【除法求值】【并查集】
2022-06-26 08:30:00 【星光技术人】
力扣399【除法求值】【并查集】

- 思路
1) 初始化映射,每个节点的父节点都是自己
2) 根据输入的先验条件合并集合的union函数
3)查找节点的父节点函数find,与此同时需要将树扁平化,改变搜索过程中经过的节点父节点为根节点,并更新权重
这里使用递归进行查找 - code
//寻找父亲根节点
string find(string str)
{
if (MP1[str] != str)
{
string origin = MP1[str];
MP1[str] = find(MP1[str]);
MP2[str] *= MP2[origin];
}
return MP1[str];
}
- 判断两个节点是否在一个集合里,通过两个节点的根节点判断
- code
class Solution {
public:
map<string, string> MP1;//记录节点的父亲根节点
map<string, double> MP2;//记录节点的父节点到父亲节点的权重
vector<double> res;
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
//初始化map,自己是自己的根节点
for (auto equation : equations)
{
for (auto str : equation)
{
MP1[str] = str;
MP2[str] = 1.0;
}
}
//合并根节点
for (int i = 0; i < equations.size(); i++)
{
Union(equations[i][0],equations[i][1], values[i]);
}
for (int i = 0; i < queries.size(); i++)
{
string str1 = queries[i][0];
string str2 = queries[i][1];
if (is_set(str1, str2))
res.push_back(MP2[str1] / MP2[str2]);
else
res.push_back(-1.0);
}
return res;
}
//合并
void Union(string str1,string str2,double w)
{
string son = find(str1);
string father = find(str2);
if (father == son)
return;
MP1[son] = father;
MP2[son] = w * MP2[str2] / MP2[str1];
}
//寻找父亲根节点
string find(string str)
{
if (MP1[str] != str)
{
string origin = MP1[str];
MP1[str] = find(MP1[str]);
MP2[str] *= MP2[origin];
}
return MP1[str];
}
//判断是否在一个集合
bool is_set(string str1, string str2)
{
if (MP1.find(str1) == MP1.end() || MP1.find(str2) == MP1.end())
{
return false;
}
if (find(str1) == find(str2))
return true;
return false;
}
};
注意事项
我在写这道题的时候在这个地方犯了错
边栏推荐
- Steps for ROS to introduce opencv (for cmakelist)
- [已解决]setOnNavigationItemSelectedListener()被弃用
- Relationship extraction --r-bert
- opencv学习笔记二
- ZLMediaKit推流拉流测试
- opencv学习笔记三
- Tokenizer description in Bert
- Degree of freedom analysis_ nanyangjx
- (4) Independent key
- Record the problem yaml file contains Chinese message 'GBK' error
猜你喜欢

Koa_mySQL_Ts 的整合

Clion installation + MinGW configuration + opencv installation

First character that appears only once

框架跳转导致定位失败的解决方法

Install Anaconda + NVIDIA graphics card driver + pytorch under win10_ gpu

What is Qi certification Qi certification process

STM32 project design: an e-reader making tutorial based on stm32f4

Apple motherboard decoding chip, lightning Apple motherboard decoding I.C

Opencv learning notes 3

opencv學習筆記三
随机推荐
I Summary Preface
框架跳转导致定位失败的解决方法
Euler function: find the number of numbers less than or equal to N and coprime with n
keras_ Callback function summary
VS2005 compiles libcurl to normaliz Solution of Lib missing
Bezier curve learning
Steps for ROS to introduce opencv (for cmakelist)
1.27 pytorch learning
Timer code guide in optee
Trimming_ nanyangjx
STM32 project design: an e-reader making tutorial based on stm32f4
Recovering the system with Clonezilla USB disk
SOC wireless charging scheme
(4) Independent key
Is it safe to open an account in flush,
Realizing sequence annotation with transformers
Time functions supported in optee
Using transformers of hugging face to realize named entity recognition
opencv学习笔记二
Golang JSON unsupported value: Nan processing