
Disjoint Union Set 并查集
???????????????????????????????????????????????????????????? ?????????????????A/B = k????????????????A?B?????????????????? ??????????????x/y?????x?y??????????????????????-1.0????????????????? ?????? ???? ?????
发布日期:2021-05-07 23:34:43
浏览次数:22
分类:精选文章
本文共 2342 字,大约阅读时间需要 7 分钟。
??????????????????????????????????????????????????????????????????????????????Union-Find??????????????????
????
????
#include#include #include using namespace std;class Solution {private: struct DsjSet { unordered_map graph; unordered_map weight; pair find(string u) { if (graph[u] == u) { return {u, 1.0}; } string orig_u = u; u = graph[u]; auto res = find(u); double product = weight[orig_u] * res.second; graph[orig_u] = res.first; return {res.first, product}; } void addEdge(string u, string v, double k) { if (graph.find(u) == graph.end()) { graph[u] = u; weight[u] = 1.0; } if (graph.find(v) == graph.end()) { graph[v] = v; weight[v] = 1.0; } auto u_root = find(u); auto v_root = find(v); if (u_root.first == v_root.first) { weight[u] *= k / weight[v]; weight[v] *= k / weight[u]; return; } graph[u_root.first] = v_root.first; weight[u_root.first] *= k * weight[u_root.first] / weight[v_root.first]; } double getWeight(string u, string v) { auto u_root = find(u); auto v_root = find(v); if (u_root.first != v_root.first) { return -1.0; } return u_root.second / v_root.second; } }; public: vector calcEquation(vector > equations, vector values, vector > queries) { DsjSet graph; int N = equations.size(); for (int i = 0; i < N; ++i) { string u = equations[i].first; string v = equations[i].second; double k = values[i]; graph.addEdge(u, v, k); } vector result; for (auto& query : queries) { string u = query.first; string v = query.second; result.push_back(graph.getWeight(u, v)); } return result; } }; int main() { Solution sol; vector > equations = { {"a","b"}, {"b","c"} }; vector values = {2.0, 3.0}; vector > queries = { {"a","c"}, {"b","a"}, {"a","e"}, {"a","a"}, {"x","x"} }; auto result = sol.calcEquation(equations, values, queries); for (auto& res : result) { cout << res << " "; } cout << endl; return 0; }
????
DsjSet
??????????????find
?????????????????????????addEdge
????????????????????????????????getWeight
?????????????????????????????????-1.0?????????????????????????????????????????????????
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年04月25日 01时38分44秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
2025版最新黑客学习网站(非常详细),零基础入门到精通,看这一篇就够了
2023-01-25
2025版网络工程11个高含金量证书(非常详细)零基础入门到精通,收藏这篇就够了
2023-01-25
2025自学成为黑客必读的5本书籍,带你从小白进阶成大佬
2023-01-25
20万高薪专业-网络安全(非常详细)零基础入门到精通,收藏这一篇就够了
2023-01-25
23张图告诉你组建一个网络需要用到哪些硬件设备?路由器、交换机、防火墙是不是就够了?
2023-01-25
24 WEB漏洞-文件上传之WAF绕过及安全修复_阿里云盾waf绕过怎么修复
2023-01-25
#12 btrfs文件系统
2023-01-25
#3194. 去月球
2023-01-25
24.线程
2023-01-25
#Leetcode# 28. Implement strStr()
2023-01-25
2022年课时四Servlet 中常用<Servlet>常用对象
2023-01-25
$route 和 $router详解、区别、示例代码
2023-01-25
$scope angular在controller之外调用
2023-01-25
&和&&的区别
2023-01-25
064:vue+openlayers根据坐标来显示点、线段、圆形、多边形
2023-01-25
(ios实战)单个ViewControl适配不同ios版本xib文件实现
2023-01-25
(Leetcode-字符串-2) 字符串运算
2023-01-25