我在我的代码中发布了一个问题,其唯一的#include指令如下:
#include <bits/stdc++.h>
Run Code Online (Sandbox Code Playgroud)
我的老师告诉我这样做,但在评论部分,我被告知我不应该这样做.
为什么?
c++ portability c++-faq turbo-c++ implementation-defined-behavior
我需要使用boost :: disjoint_sets,但文档对我来说不清楚.有人可以解释每个模板参数的含义,也许可以给出一个用于创建disjoint_sets的小例子代码吗?
根据请求,我使用disjoint_sets来实现Tarjan的离线最小共同祖先算法,即 - 值类型应该是vertex_descriptor.
我正在尝试实现Disjoint Sets以用于Kruskal的算法,但我无法准确理解它应该如何完成,特别是如何管理树林.在阅读了维基百科关于不相交集的描述之后,在阅读了算法导论(Cormen等人)中的描述后,我得出以下结论:
class DisjointSet
{
public:
class Node
{
public:
int data;
int rank;
Node* parent;
Node() : data(0),
rank(0),
parent(this) { } // the constructor does MakeSet
};
Node* find(Node*);
Node* merge(Node*, Node*); // Union
};
DisjointSet::Node* DisjointSet::find(DisjointSet::Node* n)
{
if (n != n->parent) {
n->parent = find(n->parent);
}
return n->parent;
}
DisjointSet::Node* DisjointSet::merge(DisjointSet::Node* x,
DisjointSet::Node* y)
{
x = find(x);
y = find(y);
if (x->rank > y->rank) {
y->parent = x;
} else {
x->parent = y;
if …Run Code Online (Sandbox Code Playgroud)