当前位置:网站首页>力扣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;
}
};
注意事项
我在写这道题的时候在这个地方犯了错
边栏推荐
- Tensorboard
- Intra class data member initialization of static const and static constexpr
- 1.20 study univariate linear regression
- Relation extraction model -- spit model
- How to realize wireless Ethernet high-speed communication for multiple Mitsubishi PLCs?
- ROS learning notes (5) -- Exploration of customized messages
- Installation of jupyter
- STM32 project design: temperature, humidity and air quality alarm, sharing source code and PCB
- Degree of freedom analysis_ nanyangjx
- 73b2d wireless charging and receiving chip scheme
猜你喜欢

Koa_mySQL_Ts 的整合

Use a switch to control the lighting and extinguishing of LEP lamp

Structure diagram of target detection network

(2) Buzzer

QT_ AI

Trimming_ nanyangjx

static const与static constexpr的类内数据成员初始化

First character that appears only once

STM32 project design: smart door lock PCB and source code based on stm32f1 (4 unlocking methods)

73b2d wireless charging and receiving chip scheme
随机推荐
Relation extraction model -- spit model
Text to SQL model ----irnet
FFmpeg音视频播放器实现
爬虫 对 Get/Post 请求时遇到编码问题的解决方案
opencv学习笔记三
VS2005 project call free() compiled with static libcurl library reported heap error
[resolved]setonnavigationitemselectedlistener() deprecated
Application of wireless charging receiving chip xs016 coffee mixing cup
Tokenizer description in Bert
Embedded Software Engineer (6-15k) written examination interview experience sharing (fresh graduates)
Playing card image segmentation
Zlmediakit push pull flow test
The best time to buy and sell stocks to get the maximum return
Koa_ mySQL_ Integration of TS
Deploy wiki system Wiki in kubesphere JS and enable Chinese full-text retrieval
1.17 daily improvement of winter vacation learning (frequency school and Bayesian school) and maximum likelihood estimation
Apple motherboard decoding chip, lightning Apple motherboard decoding I.C
Recovering the system with Clonezilla USB disk
Use of PCL
Tensorboard