当前位置:网站首页>Li Kou 399 [division evaluation] [joint query]
Li Kou 399 [division evaluation] [joint query]
2022-06-26 09:07:00 【Starlight technician】
Power button 399【 Divide and evaluate 】【 Union checking set 】

- Ideas
1) Initialize mapping , The parent node of each node is itself
2) Merges sets of according to the entered prior conditions union function
3) Find the parent node function of the node find, At the same time, we need to flatten the tree , Change the parent node of the node passed in the search process to the root node , And update the weight
Here we use recursion to find - code
// Find parent root node
string find(string str)
{
if (MP1[str] != str)
{
string origin = MP1[str];
MP1[str] = find(MP1[str]);
MP2[str] *= MP2[origin];
}
return MP1[str];
}
- Determine whether two nodes are in a set , Judge by the root node of the two nodes
- code
class Solution {
public:
map<string, string> MP1;// Record the parent root node of the node
map<string, double> MP2;// Record the weight from the parent node to the parent node
vector<double> res;
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
// initialization map, You are your own root node
for (auto equation : equations)
{
for (auto str : equation)
{
MP1[str] = str;
MP2[str] = 1.0;
}
}
// Merge the root node
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;
}
// Merge
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];
}
// Find parent root node
string find(string str)
{
if (MP1[str] != str)
{
string origin = MP1[str];
MP1[str] = find(MP1[str]);
MP2[str] *= MP2[origin];
}
return MP1[str];
}
// Determine whether it is in a set
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;
}
};
matters needing attention
I made a mistake in this place when I was writing this question 
边栏推荐
- Yolov5 advanced zero environment rapid creation and testing
- commonJS和ES6模块化的区别
- 1.27 pytorch learning
- SRv6----IS-IS扩展
- 在哪个软件上开户比较安全
- Tensor
- 2021 software university ranking crawler program
- Drawing with MATLAB (2) -- color ring
- MySQL cannot be found in the service (not uninstalled)
- Fix the problem that the rich text component of the applet does not support the properties of video cover, autoplay, controls, etc
猜你喜欢

Drawing with MATLAB (1)

百度小程序富文本解析工具bdParse

20220623 getting started with Adobe Illustrator

Segmentation of structured light images using segmentation network

phpcms小程序插件api接口升级到4.3(新增批量获取接口、搜索接口等)

Uniapp uses uparse to parse the content of the background rich text editor and modify the uparse style

Matlab drawing checkerboard (camera calibration)

力扣399【除法求值】【并查集】

深度学习论文阅读目标检测篇(七)中文版:YOLOv4《Optimal Speed and Accuracy of Object Detection》

Yolov5 advanced III training environment
随机推荐
1.17 daily improvement of winter vacation learning (frequency school and Bayesian school) and maximum likelihood estimation
Sqoop merge usage
How to handle the small program tabbar that does not support parameter transfer
Slider verification - personal test (JD)
Phpcms mobile station module implements custom pseudo static settings
[QNX Hypervisor 2.2用户手册]12.2 术语(二)
[qnx hypervisor 2.2 user manual]12.2 terminology (II)
【IVI】15.1.2 系统稳定性优化篇(LMKD Ⅱ)PSI 压力失速信息
Section IV HQL execution process
1.23 neural network
Mongodb分片环境搭建和验证(redis期末大作业)
隐藏式列表菜单以及窗口转换在Selenium 中的应用
phpcms v9后台文章列表增加一键推送到百度功能
Construction and verification of mongodb sharding environment (redis final assignment)
关于小程序tabbar不支持传参的处理办法
Load other related resources or configurations (promise application of the applet) before loading the homepage of the applet
编程训练7-日期转换问题
Phpcms V9 mobile phone access computer station one-to-one jump to the corresponding mobile phone station page plug-in
20220623 getting started with Adobe Illustrator
ThreadLocal