当前位置:网站首页>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 
边栏推荐
- phpcms小程序插件api接口升级到4.3(新增批量获取接口、搜索接口等)
- 远程工作的一些命令
- Speckle denoising method for ultrasonic image
- 关于小程序tabbar不支持传参的处理办法
- Some commands for remote work
- [IVI] 15.1.2 system stability optimization (lmkd Ⅱ) psi pressure stall information
- [Matlab GUI] key ID lookup table in keyboard callback
- 实践是成为网工最快的方法,网络工程师实战项目整理
- In depth study paper reading target detection (VII) Chinese version: yolov4 optimal speed and accuracy of object detection
- How to convert wechat applet into Baidu applet
猜你喜欢

Selenium builds cookies pool to bypass authentication and anti crawl login

Phpcms applet plug-in version 4.0 was officially launched

Matlab drawing checkerboard (camera calibration)

设置QCheckbox 样式的注意事项

Live review | smardaten lihongfei interprets the Research Report on China's low / no code industry: the wind direction has changed

Srv6---is-is extension

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

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

Practice is the fastest way to become a network engineer

Mongodb分片环境搭建和验证(redis期末大作业)
随机推荐
Drawing with MATLAB (2) -- color ring
报错ImportError: numpy.core.multiarray failed to import
Pytorch build progression
How to set the shelves and windows, and what to pay attention to in the optimization process
Segmentation of structured light images using segmentation network
Solution to the encoding problem encountered by the crawler when requesting get/post
Yolov5进阶之四训练自己的数据集
Yolov5进阶之二安装labelImg
【MATLAB GUI】 键盘回调中按键识别符查找表
Data warehouse (1) what is data warehouse and what are the characteristics of data warehouse
Yolov5 advanced zero environment rapid creation and testing
Sublime Text3 common plug-ins
什么是乐观锁,什么是悲观锁
Uniapp uses uparse to parse the content of the background rich text editor and modify the uparse style
设置QCheckbox 样式的注意事项
SRv6----IS-IS扩展
Differences between commonjs and ES6 modularity
Load other related resources or configurations (promise application of the applet) before loading the homepage of the applet
How to convert wechat applet into Baidu applet
【IVI】15.1.2 系统稳定性优化篇(LMKD Ⅱ)PSI 压力失速信息