This documentation is automatically generated by competitive-verifier/competitive-verifier
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_4_A"
#include "../../graph/topological_sort.hpp"
#include "../../template/template.hpp"
int main() {
int N, M;
cin >> N >> M;
TopologicalSort ts(N);
rep (i, 0, M) {
int u, v;
cin >> u >> v;
ts.add_edge(u, v);
}
ts.build();
cout << (ts.is_dag() ? 0 : 1) << LF;
return 0;
}
#line 1 "test/graph/topological_sort.is_dag.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_4_A"
#line 2 "graph/topological_sort.hpp"
#line 2 "template/template.hpp"
/**
* @author ku_senjan
* @title 提出用テンプレート
* @see https://github.com/kogetsu0728/ku-library
*/
#line 2 "template/constant.hpp"
#line 2 "template/include.hpp"
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#line 2 "template/type_alias.hpp"
#line 4 "template/type_alias.hpp"
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T, bool REVERSE = false>
using heap = priority_queue<T, vector<T>, conditional_t<REVERSE, greater<T>, less<T>>>;
#line 5 "template/constant.hpp"
template <class T>
inline constexpr T INF = numeric_limits<T>::max() / 2;
inline constexpr array<int, 4> DY4 = {0, -1, 0, 1};
inline constexpr array<int, 4> DX4 = {1, 0, -1, 0};
inline constexpr array<int, 8> DY8 = {0, -1, -1, -1, 0, 1, 1, 1};
inline constexpr array<int, 8> DX8 = {1, 1, 0, -1, -1, -1, 0, 1};
inline constexpr char LF = '\n';
#line 2 "template/macro.hpp"
#line 5 "template/macro.hpp"
/**
* @see https://trap.jp/post/1224/
*/
#ifdef LOCAL
inline constexpr bool IS_LOCAL = true;
#else
inline constexpr bool IS_LOCAL = false;
#endif
#define IF_LOCAL if constexpr (IS_LOCAL)
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define overload4(a, b, c, d, e, ...) e
#define rep1(i, a) for (ll i = 0; (i) < (ll)(a); ++(i))
#define rep2(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); ++(i))
#define rep3(i, a, b, c) for (ll i = (ll)(a); (i) < (ll)(b); (i) += (ll)(c))
#define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(i, a) for (ll i = (ll)(a); (i) >= 0; --(i))
#define rrep2(i, a, b) for (ll i = (ll)(a); (i) >= (ll)(b); --(i))
#define rrep3(i, a, b, c) for (ll i = (ll)(a); (i) >= (ll)(b); (i) -= (ll)(c))
#define rrep(...) overload4(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#line 13 "template/template.hpp"
#line 2 "utility/choose_min_max.hpp"
/**
* @title Choose Minimum / Maximum
*/
template <class T>
bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#line 2 "utility/set_io.hpp"
#line 4 "utility/set_io.hpp"
void set_io(int d = 16) {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(d);
return;
}
#line 4 "graph/topological_sort.hpp"
/**
* @brief Topological Sort (トポロジカルソート)
* @note 参考: https://algo-logic.info/topological-sort/
*/
class TopologicalSort {
private:
bool init, dag;
int n;
vector<vector<int>> g;
vector<int> p;
public:
TopologicalSort() : TopologicalSort(0) {}
TopologicalSort(int _n) : init(false), dag(false), n(_n), g(_n), p(0) {}
void add_edge(int u, int v) noexcept {
assert(!init);
g[u].emplace_back(v);
}
void build() noexcept {
assert(!init);
init = true;
vector<int> cnt(n);
rep (v, 0, n) {
for (int nv : g[v]) {
cnt[nv]++;
}
}
queue<int> que;
rep (v, 0, n) {
if (cnt[v] == 0) {
que.emplace(v);
}
}
p.reserve(n);
while (!que.empty()) {
int v = que.front();
que.pop();
p.emplace_back(v);
for (int nv : g[v]) {
cnt[nv]--;
if (cnt[nv] == 0) {
que.emplace(nv);
}
}
}
if (int(p.size()) == n) {
dag = true;
} else {
p.clear();
}
return;
}
bool is_dag() const {
assert(init);
return dag;
}
int get(int i) const {
assert(init);
assert(dag);
return p[i];
}
};
#line 5 "test/graph/topological_sort.is_dag.test.cpp"
int main() {
int N, M;
cin >> N >> M;
TopologicalSort ts(N);
rep (i, 0, M) {
int u, v;
cin >> u >> v;
ts.add_edge(u, v);
}
ts.build();
cout << (ts.is_dag() ? 0 : 1) << LF;
return 0;
}
| Env | Name | Status | Elapsed | Memory |
|---|---|---|---|---|
| g++ | 00_small_00.in |
|
5 ms | 3 MB |
| g++ | 00_small_01.in |
|
4 ms | 4 MB |
| g++ | 00_small_02.in |
|
4 ms | 4 MB |
| g++ | 00_small_03.in |
|
4 ms | 4 MB |
| g++ | 01_medium_00.in |
|
4 ms | 4 MB |
| g++ | 01_medium_01.in |
|
4 ms | 3 MB |
| g++ | 01_medium_02.in |
|
4 ms | 3 MB |
| g++ | 01_medium_03.in |
|
4 ms | 3 MB |
| g++ | 02_components_00.in |
|
4 ms | 3 MB |
| g++ | 02_components_01.in |
|
4 ms | 4 MB |
| g++ | 02_components_02.in |
|
4 ms | 4 MB |
| g++ | 02_components_03.in |
|
4 ms | 3 MB |
| g++ | 03_grid_00.in |
|
4 ms | 3 MB |
| g++ | 03_grid_01.in |
|
4 ms | 4 MB |
| g++ | 03_grid_02.in |
|
4 ms | 4 MB |
| g++ | 03_grid_03.in |
|
4 ms | 4 MB |
| g++ | 04_linear_00.in |
|
4 ms | 4 MB |
| g++ | 04_linear_01.in |
|
4 ms | 4 MB |
| g++ | 04_linear_02.in |
|
4 ms | 4 MB |
| g++ | 04_linear_03.in |
|
4 ms | 3 MB |
| g++ | 05_large_00.in |
|
4 ms | 3 MB |
| g++ | 05_large_01.in |
|
4 ms | 3 MB |
| g++ | 05_large_02.in |
|
4 ms | 4 MB |
| g++ | 05_large_03.in |
|
4 ms | 4 MB |
| g++ | 06_critical_00.in |
|
4 ms | 3 MB |
| g++ | 06_critical_01.in |
|
4 ms | 3 MB |
| g++ | 06_critical_02.in |
|
4 ms | 3 MB |
| g++ | 06_critical_03.in |
|
4 ms | 4 MB |
| g++ | 07_rand_00.in |
|
4 ms | 4 MB |
| g++ | 07_rand_01.in |
|
4 ms | 3 MB |
| g++ | 07_rand_02.in |
|
4 ms | 4 MB |
| g++ | 07_rand_03.in |
|
4 ms | 3 MB |
| g++ | 07_rand_04.in |
|
4 ms | 4 MB |
| g++ | 07_rand_05.in |
|
4 ms | 4 MB |
| g++ | 07_rand_06.in |
|
4 ms | 3 MB |
| g++ | 07_rand_07.in |
|
4 ms | 4 MB |
| g++ | 08_dag_00.in |
|
4 ms | 3 MB |
| g++ | 08_dag_01.in |
|
4 ms | 3 MB |
| g++ | 08_dag_02.in |
|
4 ms | 3 MB |
| g++ | 08_dag_03.in |
|
4 ms | 3 MB |
| g++ | 09_maximum_00.in |
|
4 ms | 4 MB |
| g++ | 09_maximum_01.in |
|
4 ms | 4 MB |
| g++ | 09_maximum_02.in |
|
4 ms | 3 MB |
| g++ | 09_maximum_03.in |
|
4 ms | 3 MB |