#include <iostream>                  // for std::cout
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/visitors.hpp>

using namespace boost;

struct cycle_detector: public dfs_visitor<> {
    cycle_detector(bool& has_cycle) :
            _has_cycle(has_cycle) {
    }

    template<class Edge, class Graph>
    void back_edge(Edge, Graph&) {
        _has_cycle = true;
    }
protected:
    bool& _has_cycle;
};

int main(int, char*[]) {
// create a typedef for the Graph type
    typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;

    // Make convenient labels for the vertices
    enum {
        A, B, C, N
    };
    const int num_vertices = N;

// writing out the edges in the graph
    typedef std::pair<int, int> Edge;
    Edge edge_array[] = {
            Edge(A, B), Edge(C, B), Edge(A, C)
    };
    const int num_edges = sizeof(edge_array) / sizeof(edge_array[0]);

// declare a graph object
    Graph g(num_vertices);

// add the edges to the graph object
    for (int i = 0; i < num_edges; ++i)
        add_edge(edge_array[i].first, edge_array[i].second, g);

    bool has_cycle = false;
    cycle_detector vis(has_cycle);
    boost::depth_first_search(g, visitor(vis));
    std::cerr << "has_cycle: " << std::boolalpha << has_cycle << std::endl;
    return 0;
}