#ifndef GRAPH_H #define GRAPH_H #include #include #include #include class Graph { public: Graph() : vertices_(), graph_() {} void add_vertex(int vertex) { if (vertices_.find(vertex) == vertices_.end()) { vertices_.insert(vertex); graph_[vertex] = std::vector(); } } void add_edge(int from, int to) { add_vertex(from); add_vertex(to); graph_[from].push_back(to); has_out_edges_.insert(from); } const std::set & get_vertices() const { return vertices_; } const std::vector & get_neighbors(int vertex) const { return graph_.find(vertex)->second; } bool has_out_edges(int vertex) const { return has_out_edges_.count(vertex) > 0; } private: std::set vertices_; std::unordered_map> graph_; std::unordered_set has_out_edges_; }; #endif