本文整理汇总了C++中DisjointSet::size方法的典型用法代码示例。如果您正苦于以下问题:C++ DisjointSet::size方法的具体用法?C++ DisjointSet::size怎么用?C++ DisjointSet::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DisjointSet
的用法示例。
在下文中一共展示了DisjointSet::size方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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();
}
示例2: main
int main(int argc, char *argv[]) {
DisjointSet ds;
for (int set_number=0; set_number<DSET_SIZE; ++set_number) {
ds.MakeSet(set_number);
}
srand(time(NULL));
int unions = 0;
int attempts = 0;
while (ds.size() > 1) {
// generate random couples and do Union operations, up to when there is
// only a single set left.
int x = rand() % DSET_SIZE;
int y = rand() % DSET_SIZE;
if (ds.Union(x,y)) {
unions += 1;
} else {
attempts += 1;
}
//std::cout << ds.toDot() << std::endl;
}
std::cerr << "unions=" << unions << ", attempts=" << attempts << std::endl;
std::cout << ds.toDot() << std::endl;
return 0;
}
示例3: ds
#define DISJOINT_SET_TEST
#include "third_party/catch.hpp"
#include "data_structures/disjoint_set.cpp"
TEST_CASE("Create a large set containing 10^8 elements", "[disjoint-set]") {
/*
Note: This test requires around 800MB of memory.
*/
DisjointSet ds(1e8);
REQUIRE(ds.size() == 1e8);
}
TEST_CASE("Check initial state", "[disjoint-set]") {
DisjointSet ds(20);
for (size_t element = 0; element < ds.size(); element++) {
REQUIRE(ds.find(element) == element);
}
}
TEST_CASE("Connect elements and verify their representatives", "[disjoint-set]") {
DisjointSet ds(16); // create a disjoint-set with elements from 0 to 15
ds.join(0, 8);
REQUIRE(ds.find(0) == 0);
REQUIRE(ds.find(8) == 0);
ds.join(3, 15);
REQUIRE(ds.find(3) == 3);
REQUIRE(ds.find(15) == 3);