联合查找算法的实现 [英] Implementation of Union find Algorithm

查看:71
本文介绍了联合查找算法的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的键值对.每个键有2个值.键的值可能相符.
最初,我从Key1开始,ValueKey1,ValueKey2存储在一个集中.(也许是数组).

现在,对于每个连续的Keyi,我检查Valuei,Valuei + 1是否在集合中.如果其中任何一个在集合中,请加入集合.

否则,如果两个KeyValue都存在于集合中,则丢弃该键.


我如何用C ++实现这个东西,我的想法很少,所以我建议如果可能的话,代码片段或提示会真的很有帮助.

I have a series of Key value pairs. Each Key has 2 values. Values of Keys ma coincide.
Initially i start with Key1, ValueKey1, ValueKey2 are stored in a set.(Maybe Array).

Now for each consecutive Keyi, i check if Valuei, Valuei+1 are in the set. If any one of them is in the set, then join the set.

Else if both KeyValues are present in a set, discard that key .


How can i implement this thing using C++, i have very less idea, so i would suggest if possible a code snippet or a hint would be really helpful.

推荐答案

A暗示?好的,我会给您一个提示,但是首先,请看我对问题的评论,并意识到您对问题的表述不清楚.

现在,有一个提示:您只需要实现集合理论操作(例如,union(析取)).只需通过该操作的定义 即可实现.

尝试一下,如果遇到任何问题,请向我们展示您的代码;如果您还解释您的困难所在,我们可能会为您提供帮助.

—SA
A hint? OK, I''ll give a hint, but first, see my comment to the question and realize that your formulation of the problem is not clear.

Now, a hint: you simply need implementation of set-theory operation like union (disjunction). Simply implement it by definition of that operation.

Try it and show us your code if you face any problems; if you also explain where is your difficulty, we can probably help you.

—SA


这篇关于联合查找算法的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆