lmori's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub lmorinn/library

:heavy_check_mark: verify/LibraryChecker/graph/others/CycleDetectionDirected.test.cpp

Depends on

Code

#include "../../../../template/template.hpp"
#define PROBLEM "https://judge.yosupo.jp/problem/cycle_detection"
#include "../../../../graph/others/CycleDetection.hpp"

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    in(n, m);
    CycleDetection<int> g(n, true);
    rep(i, m) {
        int u, v;
        in(u, v);
        g.add_edge(u, v, i);
    }

    auto res = g.cycle();
    int len = res.size();
    if (len == 0) {
        out(-1);
    } else {
        out(len);
        rep(i, len) {
            out(res[i].val);
        }
    }
}
#line 2 "template/template.hpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

void outf() { cout << '\n'; }
template <class T, class... Ts>
void outf(const T &a, const Ts &...b) {
  cout << fixed << setprecision(14) << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion
#line 2 "verify/LibraryChecker/graph/others/CycleDetectionDirected.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/cycle_detection"
#line 1 "graph/others/CycleDetection.hpp"
template <class T>
class CycleDetection {
   private:
    struct Edge {
        int from = -1, to = -1;
        T val;
        Edge() {}
        Edge(int f, int t, T v) {
            from = f;
            to = t;
            val = v;
        }
    };

    vector<vector<Edge>> g;
    vector<unordered_map<int, int>> undirected_id;
    int n;
    bool is_directed;
    bool ud_two_edge_cycle = false;
    stack<Edge> history;
    pair<int, int> two_v;
    T two_e;
    vector<bool> seen, finished;

    int dfs(int cur, Edge cur_e, bool is_prohibit_reverse) {
        seen[cur] = true;
        history.emplace(cur_e);
        for (const Edge &nex : g[cur]) {
            if (is_prohibit_reverse and cur_e.from == nex.to) continue;
            if (finished[nex.to]) continue;
            if (seen[nex.to] and !finished[nex.to]) {
                history.emplace(nex);
                return nex.to;
            }
            int pos = dfs(nex.to, nex, is_prohibit_reverse);
            if (pos != -1) return pos;
        }
        finished[cur] = true;
        history.pop();
        return -1;
    }

    vector<Edge> reconstruct(int pos) {
        vector<Edge> cycle;
        while (!history.empty()) {
            const Edge &e = history.top();
            history.pop();
            cycle.emplace_back(e);
            if (e.from == pos) break;
        }
        reverse(cycle.begin(), cycle.end());
        return cycle;
    }

    vector<Edge> detect(bool is_prohibit_reverse = true) {
        seen.assign(n, false);
        finished.assign(n, false);
        int pos = -1;
        for (int cur = 0; cur < n and pos == -1; cur++) {
            if (seen[cur]) continue;
            while (!history.empty()) history.pop();
            pos = dfs(cur, Edge(), is_prohibit_reverse);
            if (pos != -1) return reconstruct(pos);
        }
        return vector<Edge>();
    }

   public:
    CycleDetection(int siz, bool is_directed_graph) {
        n = siz;
        is_directed = is_directed_graph;
        g.resize(n);
        if (!is_directed) undirected_id.resize(n);
    }

    void add_edge(int u, int v, T w) {
        if (is_directed) {
            g[u].emplace_back(Edge(u, v, w));
        } else {
            if (u > v) swap(u, v);
            if (undirected_id[u].contains(v)) {
                ud_two_edge_cycle = true;
                two_v = {u, v};
                two_e = w;
            } else {
                undirected_id[u][v] = w;
            }
            g[u].emplace_back(Edge(u, v, w));
            g[v].emplace_back(Edge(v, u, w));
        }
    }

    vector<Edge> cycle() {
        if (is_directed) {
            return detect(false);
        } else if (ud_two_edge_cycle) {
            int u = two_v.first;
            int v = two_v.second;
            vector<Edge> res = {Edge(u, v, undirected_id[u][v]), Edge(v, u, two_e)};
            return res;
        } else {
            return detect(true);
        }
    }
};
#line 4 "verify/LibraryChecker/graph/others/CycleDetectionDirected.test.cpp"

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    in(n, m);
    CycleDetection<int> g(n, true);
    rep(i, m) {
        int u, v;
        in(u, v);
        g.add_edge(u, v, i);
    }

    auto res = g.cycle();
    int len = res.size();
    if (len == 0) {
        out(-1);
    } else {
        out(len);
        rep(i, len) {
            out(res[i].val);
        }
    }
}
Back to top page