This documentation is automatically generated by online-judge-tools/verification-helper
// 結果が一意でないため、テストは行わない
// #define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_4_B"
#include <bits/stdc++.h>
#include "ugilib/base/constants.hpp"
#include "ugilib/base/definitions.hpp"
#include "ugilib/graph/topological_sort.hpp"
using namespace std;
// debug settings
// #define DEBUG
#ifdef DEBUG
// debug input
string _INPUT = R"(
5
1 2 3 4 5
)";
auto _cin = stringstream(_INPUT.substr(1)); // remove '\n' at _INPUT[0]
#else
// standard input
auto& _cin = cin;
#endif
// speed up
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// reader
struct rd {
// T
template<typename T> static T i() { T x; _cin >> x; return x; } // T item
// vector<T>
template<typename T> static vector<T> v(int n) {vector<T> v(n); rep(i, n) _cin >> v[i]; return v;} // vector<T>
// vector<pair<T, T>>
template<typename T> static vector<pair<T, T>> vp(int n) {vector<pair<T, T>> v(n); rep(i, n) _cin >> v[i].first >> v[i].second; return v;} // vector<pair<T, T>>
// tuple
template<typename... Args> static tuple<Args...> t() {
tuple<Args...> values;
apply([](auto&... args) { ((_cin >> args), ...); }, values);
return values;
}
};
// debug print utility
namespace deb {
#include <cxxabi.h>
// demangle type name
string demangle(const char* name) {
int status = -4;
unique_ptr<char, void(*)(void*)> res{
abi::__cxa_demangle(name, NULL, NULL, &status),
free
};
return (status == 0) ? string(res.get()) : name ;
}
// meta functions for type traits
template<typename T>
constexpr bool isArithmeticContainer() { return is_arithmetic<typename T::value_type>::value; }
// for SFINAE
template<typename T, typename = void> struct has_key_and_mapped_type : false_type {};
template<typename T> struct has_key_and_mapped_type<T, void_t<typename T::key_type, typename T::mapped_type>> : true_type {};
// for map or unordered_map
template<typename T>
constexpr bool isMapLike() { return has_key_and_mapped_type<T>::value; }
// for values
template<typename T, typename enable_if<is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& x) { cout << x << " "; }
// for pairs
template <typename T, typename S>
void p(const pair<T, S>& _p){ p(_p.first); p(_p.second); cout << endl; }
// for string
void p(const string& s) { cout << s << endl; }
// for containers
template<typename T, typename enable_if<!is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& container) {
// map and unordered_map
if constexpr (isMapLike<T>()) {
cout << demangle(typeid(T).name()) << ":" << endl;
for (const auto& kv : container) {
cout << "[" << kv.first << "] => ";
p(kv.second);
if constexpr (is_arithmetic_v<typename T::mapped_type>) cout << endl;
}
// vector or set or others
} else {
if constexpr (!isArithmeticContainer<T>()) cout << demangle(typeid(T).name()) << ":" << endl;
for (auto it = begin(container); it != end(container); ++it) {
p(*it);
}
if constexpr (isArithmeticContainer<T>()) cout << endl;
}
}
}; // namespace deb
int main() {
auto& cin = _cin;
// speed up io
cin.tie(nullptr);
ios::sync_with_stdio(false);
// code
auto [N, M] = rd::t<ll, ll>();
vector<vector<int>> g(N);
rep(i, M) {
auto [x, y] = rd::t<int, int>();
g[x].push_back(y);
}
vector<int> topology = ugilib::topological_sort(g);
for (int node : topology) cout << node << endl;
return 0;
}
#line 1 "tests/graph/topological_sort.notest.cpp"
// 結果が一意でないため、テストは行わない
// #define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_4_B"
#include <bits/stdc++.h>
#line 2 "ugilib/base/definitions.hpp"
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define rep(i, n) for(size_t i = 0; i < n; i++) // rep macro
#define all(v) begin(v), end(v) // all iterator
#line 3 "ugilib/base/constants.hpp"
namespace ugilib::constants {
template<typename T>
inline constexpr T INF = std::numeric_limits<T>::max() / 2;
} // namespace ugilib::constants
const ll INF = ugilib::constants::INF<ll>;
#line 2 "ugilib/graph/topological_sort.hpp"
/**
* @file topological_sort.hpp
* @brief トポロジカルソート,入次数計算関数の定義
*/
#line 9 "ugilib/graph/topological_sort.hpp"
using namespace std;
namespace ugilib {
/**
* @brief DAGの各頂点の入次数を計算する
* @tparam T 整数型
* @param g 有向グラフ
* @return vector<T> 入次数リスト
* @note O(V + E)
*/
template <typename T>
vector<T> calc_indegrees(const vector<vector<T>> &g) {
static_assert(is_integral<T>::value, "T must be integral type");
vector<T> indeg(g.size(), 0);
for (const vector<T> &edges_from_u : g) {
for (const T &v : edges_from_u) {
indeg[v]++;
}
}
return indeg;
}
/**
* @brief DAGのトポロジカルソート
* @tparam T 整数型
* @param g DAG
* @return vector<T> トポロジカルソート結果
* @note O(V + E)
*/
template <typename T>
vector<T> topological_sort(const vector<vector<T>> &g) {
static_assert(is_integral<T>::value, "T must be integral type");
// 入次数の計算
vector<T> indegs = ugilib::calc_indegrees(g);
// トポロジカル順序を決定する
vector<T> topology;
queue<T> start_nodes;
for (size_t i = 0; i < indegs.size(); i++) {
if (indegs[i] == 0) start_nodes.push(i);
}
while (!start_nodes.empty()) {
// 入次数0の頂点の順序を確定
T node = start_nodes.front(); start_nodes.pop();
topology.push_back(node);
// 頂点・辺を削除する
for (const T &next : g[node]) { // この頂点から出る全ての辺を削除
if (--indegs[next] == 0) { // それによって入次数が0になったら
start_nodes.push(next); // 順序を確定できる
}
}
}
assert(topology.size() == g.size()); // DAGでない場合はトポロジカルソートできない
return topology;
}
} // namespace ugilib
#line 8 "tests/graph/topological_sort.notest.cpp"
using namespace std;
// debug settings
// #define DEBUG
#ifdef DEBUG
// debug input
string _INPUT = R"(
5
1 2 3 4 5
)";
auto _cin = stringstream(_INPUT.substr(1)); // remove '\n' at _INPUT[0]
#else
// standard input
auto& _cin = cin;
#endif
// speed up
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// reader
struct rd {
// T
template<typename T> static T i() { T x; _cin >> x; return x; } // T item
// vector<T>
template<typename T> static vector<T> v(int n) {vector<T> v(n); rep(i, n) _cin >> v[i]; return v;} // vector<T>
// vector<pair<T, T>>
template<typename T> static vector<pair<T, T>> vp(int n) {vector<pair<T, T>> v(n); rep(i, n) _cin >> v[i].first >> v[i].second; return v;} // vector<pair<T, T>>
// tuple
template<typename... Args> static tuple<Args...> t() {
tuple<Args...> values;
apply([](auto&... args) { ((_cin >> args), ...); }, values);
return values;
}
};
// debug print utility
namespace deb {
#include <cxxabi.h>
// demangle type name
string demangle(const char* name) {
int status = -4;
unique_ptr<char, void(*)(void*)> res{
abi::__cxa_demangle(name, NULL, NULL, &status),
free
};
return (status == 0) ? string(res.get()) : name ;
}
// meta functions for type traits
template<typename T>
constexpr bool isArithmeticContainer() { return is_arithmetic<typename T::value_type>::value; }
// for SFINAE
template<typename T, typename = void> struct has_key_and_mapped_type : false_type {};
template<typename T> struct has_key_and_mapped_type<T, void_t<typename T::key_type, typename T::mapped_type>> : true_type {};
// for map or unordered_map
template<typename T>
constexpr bool isMapLike() { return has_key_and_mapped_type<T>::value; }
// for values
template<typename T, typename enable_if<is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& x) { cout << x << " "; }
// for pairs
template <typename T, typename S>
void p(const pair<T, S>& _p){ p(_p.first); p(_p.second); cout << endl; }
// for string
void p(const string& s) { cout << s << endl; }
// for containers
template<typename T, typename enable_if<!is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& container) {
// map and unordered_map
if constexpr (isMapLike<T>()) {
cout << demangle(typeid(T).name()) << ":" << endl;
for (const auto& kv : container) {
cout << "[" << kv.first << "] => ";
p(kv.second);
if constexpr (is_arithmetic_v<typename T::mapped_type>) cout << endl;
}
// vector or set or others
} else {
if constexpr (!isArithmeticContainer<T>()) cout << demangle(typeid(T).name()) << ":" << endl;
for (auto it = begin(container); it != end(container); ++it) {
p(*it);
}
if constexpr (isArithmeticContainer<T>()) cout << endl;
}
}
}; // namespace deb
int main() {
auto& cin = _cin;
// speed up io
cin.tie(nullptr);
ios::sync_with_stdio(false);
// code
auto [N, M] = rd::t<ll, ll>();
vector<vector<int>> g(N);
rep(i, M) {
auto [x, y] = rd::t<int, int>();
g[x].push_back(y);
}
vector<int> topology = ugilib::topological_sort(g);
for (int node : topology) cout << node << endl;
return 0;
}