本文整理汇总了C++中DisjointSet::find方法的典型用法代码示例。如果您正苦于以下问题:C++ DisjointSet::find方法的具体用法?C++ DisjointSet::find怎么用?C++ DisjointSet::find使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DisjointSet
的用法示例。
在下文中一共展示了DisjointSet::find方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: kruskal
long int kruskal(int m, vector<triple> &arestas){
DisjointSet<int> *ds = new DisjointSet<int>(m);
// vector<ii> result;
long int t = 0;
for(int i=0; i<m; i++)
ds->make(i, 0);
sort(arestas.begin(), arestas.end());
int e = 0, i = 0;
while(e < m-1){
int u = ds->find(arestas[i].first);
int v = ds->find(arestas[i].second);
if(ds->find(u) != ds->find(v)){
t += arestas[i].third;
// result.push_back(ii(u, v));
ds->join(u, v);
e++;
}
i++;
}
return t;
}
示例2: main
int main(void) {
std::cin.tie(0);
int n, p, q;
while(cin >> n && n) {
cin >> p >> q;
for(int i = 0; i < n; ++i)
cin >> pts[i].first >> pts[i].second;
vector<edge> es;
for(int i = 0; i < n; ++i)
for(int j = i+1; j < n; ++j)
es.push_back(edge(i, j, dist(i, j)));
sort(es.begin(), es.end());
DisjointSet ds = DisjointSet(n);
double ans = 0;
ds.merge(p-1, q-1);
ans += dist(p-1, q-1);
for(size_t i = 0; i < es.size(); ++i) {
if(ds.find(es[i].u) != ds.find(es[i].v)) {
ds.merge(es[i].u, es[i].v);
ans += es[i].c;
}
}
printf("%.2lf\n", ans);
}
return 0;
}
示例3: doAFind
void doAFind(DisjointSet set)
{
cout << "Do a find on each number" << endl;
for (int i = 0; i < 10; i++) {
cout << "Find on "<< i << " = " << set.find(i) << endl;
}
}
示例4: main
int main(void){
int T;
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++) {
memset(v, 0, sizeof(v));
int n, m;
scanf("%d %d", &n, &m);
DisjointSet d = DisjointSet(n + 1);
bool flag = true;
int i, j, pi = 0, pj = 0;
while(m--) {
scanf("%d %d", &i, &j);
if(!flag) continue;
v[i] = d.find(i);
v[j] = d.find(j);
if(d.find(i) == d.find(j))
flag = false;
if(d.find(pj) == v[i] || d.find(pi) == v[j]) {
d.merge(i, pj);
d.merge(j, pi);
} else {
d.merge(i, pi);
d.merge(j, pj);
}
pi = i;
pj = j;
}
printf("Scenario #%d:\n", kase);
if(flag) printf("No suspicious bugs found!\n\n");
else printf("Suspicious bugs found!\n\n");
}
return 0;
}
示例5: main
int main() {
DisjointSet DS;
DS.makeSet(0);
DS.makeSet(1);
DS.makeSet(2);
DS.makeSet(3);
DS.makeSet(4);
DS.union_sets(0, 2);
//cout << DS.find(2) << endl;
DS.union_sets(3, 4);
DS.union_sets(0, 1);
DS.union_sets(1, 2);
DS.union_sets(1, 3);
cout << DS.size() << endl;
cout << DS.find(0) << endl;
cout << DS.find(1) << endl;
cout << DS.find(2) << endl;
cout << DS.find(3) << endl;
cout << DS.find(4) << endl;
//for(int i = 0; i < DS.size(); i++)
// cout << i << "'s root: " << DS.find(i) << endl;
//DS.print();
}
示例6: kruskalRequest
void GraphController::kruskalRequest()
{
viewer->unselectEdges();
DisjointSet set;
auto result = GraphAlgorithms::getKruskalMST(*graph, &set);
if (result.empty())
{
viewer->showResult("No MST found.");
return;
}
std::unordered_map<int, int> sums;
std::vector<int> edgesIds;
for (auto& edge: result)
{
int id = set.find(edge.getVertex1());
auto it = sums.find(id);
if (it != sums.end())
it->second += edge.getWeight();
else
{
sums[id] = edge.getWeight();
}
edgesIds.push_back(edge.getId());
}
std::vector<int> nodes;
std::string st;
for (auto& it:sums)
{
st += "(" + std::to_string(it.first) +") = " + std::to_string(it.second) + ", ";
nodes.push_back(it.first);
}
st.erase(st.end() - 2, st.end());
viewer->selectEdges(edgesIds, nodes);
std::string s = "";
if (nodes.size() > 1)
{
s = "s";
}
viewer->showResult((std::to_string(sums.size()) +" MST found with total weight"+s+": " + st).c_str());
}