This documentation is automatically generated by online-judge-tools/verification-helper
#include "graph/topological-sort.hpp"
#pragma once
#include "graph_template.hpp"
template<typename Cost = int>
std::vector<int> topological_sort(const Graph<Cost>& G) {
int N = (int)G.size();
std::vector<int> indeg(N, 0);
for(int i = 0; i < N; i++) {
for(const auto& e : G[i]) {
indeg[e.to]++;
}
}
std::vector<int> st, ret;
for(int i = 0; i < N; i++) {
if(indeg[i] == 0) st.push_back(i);
}
while(!st.empty()) {
int u = st.back();
st.pop_back();
ret.push_back(u);
for(const auto& e : G[u]) {
indeg[e.to]--;
if(indeg[e.to] == 0) st.push_back(e.to);
}
}
return ret;
}
#line 2 "graph/topological-sort.hpp"
#line 2 "graph/graph_template.hpp"
#include <cassert>
#include <vector>
template <typename Cost = int> struct Edge {
int from, to;
Cost cost;
int id;
Edge() = default;
explicit Edge(int from, int to, Cost cost = 1, int id = -1)
: from(from), to(to), cost(cost), id(id) {}
operator int() const { return to; }
};
template <typename Cost = int> class Graph {
public:
Graph() = default;
explicit Graph(int N) : N(N), M(0), G(N) {}
inline void add_directed_edge(int from, int to, Cost cost = 1) {
assert(0 <= from && from < N);
assert(0 <= to && to < N);
G[from].emplace_back(from, to, cost, M++);
}
inline void add_undirected_edge(int from, int to, Cost cost = 1) {
assert(0 <= from && from < N);
assert(0 <= to && to < N);
G[from].emplace_back(from, to, cost, M);
G[to].emplace_back(to, from, cost, M++);
}
inline size_t size() const { return G.size(); }
inline std::vector<Edge<Cost>> &operator[](const int &i) { return G[i]; }
inline const std::vector<Edge<Cost>> &operator[](const int &i) const {
return G[i];
}
protected:
int N, M;
std::vector<std::vector<Edge<Cost>>> G;
};
template <class Cost = int> using Edges = std::vector<Edge<Cost>>;
#line 4 "graph/topological-sort.hpp"
template<typename Cost = int>
std::vector<int> topological_sort(const Graph<Cost>& G) {
int N = (int)G.size();
std::vector<int> indeg(N, 0);
for(int i = 0; i < N; i++) {
for(const auto& e : G[i]) {
indeg[e.to]++;
}
}
std::vector<int> st, ret;
for(int i = 0; i < N; i++) {
if(indeg[i] == 0) st.push_back(i);
}
while(!st.empty()) {
int u = st.back();
st.pop_back();
ret.push_back(u);
for(const auto& e : G[u]) {
indeg[e.to]--;
if(indeg[e.to] == 0) st.push_back(e.to);
}
}
return ret;
}