150 using ElementType = std::remove_reference_t<
decltype(*first)>;
151 std::unordered_set<ElementType> visited;
152 std::vector<ElementType> stack;
154 auto visit = [&](
auto& self, ElementType ¤t)
mutable ->
void {
155 visited.emplace(current);
156 for (
auto next: adjacent(current)) {
157 if (!visited.contains(next)) {
161 stack.push_back(current);
167 if (!visited.contains(*it)) {
172 std::move(stack.rbegin(), stack.rend(), first);
184 bool has_cycle(It first, It last, Adjacent adjacent, OutputIt cycle_output =
nullptr) {
185 using ElementType = std::remove_reference_t<
decltype(*first)>;
186 std::unordered_set<ElementType> visited;
187 std::unordered_set<ElementType> path;
188 auto visit = [&](
auto& self, ElementType ¤t)
mutable ->
bool {
189 visited.emplace(current);
190 path.emplace(current);
191 for (ElementType &next: adjacent(current)) {
192 if (!visited.contains(next) && self(self, next)) {
195 if (path.contains(next)) {
203 for (
auto root = first; root != last; ++root) {
204 if (!visited.contains(*root) && visit(visit, *root)) {
205 if constexpr (!std::is_same_v<OutputIt, std::nullptr_t>) {
206 std::move(path.begin(), path.end(), cycle_output);