Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/library_checker/graph/cycle_detection_undirected.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/cycle_detection_undirected"
#include "src/template/template.hpp"
#include "src/graph/graph_template.hpp"
#include "src/graph/cycle_detection.hpp"
int main(void) {
    int n, m;
    cin >> n >> m;
    Graph<int> g(n);
    rep(i, 0, m) {
        int a, b;
        cin >> a >> b;
        g.add_edge(a, b);
    }
    Edges<int> cycle = cycle_detection(g, false);
    if(cycle.empty()) {
        cout << -1 << '\n';
        return 0;
    }
    cout << cycle.size() << '\n';
    for(const Edge<int>& e : cycle) {
        cout << e.from << ' ';
    }
    cout << '\n';
    for(const Edge<int>& e : cycle) {
        cout << e.idx << ' ';
    }
    cout << '\n';
}
#line 1 "verify/library_checker/graph/cycle_detection_undirected.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/cycle_detection_undirected"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/graph/cycle_detection.hpp"
template <typename T>
Edges<T> cycle_detection(const Graph<T>& g, const bool directed = true) {
    const int n = g.size();
    for(int i = 0; i < n; ++i) {
        for(const Edge<T>& e : g[i]) {
            if(i == e.to) return {e};
        }
    }
    Edges<T> cycle;
    vector<int> pre(n, -1), visited(n, 0);
    int finish = 0;
    auto dfs = [&](const auto& dfs, const int cur, const int pval, const Edge<T>& par) -> int {
        pre[cur] = pval;
        visited[cur] = 1;
        for(const Edge<T>& e : g[cur]) {
            if(finish) return -1;
            if(!directed and e.idx == par.idx) continue;
            if(pre[e.to] == pre[cur]) {
                cycle.emplace_back(e);
                return e.to;
            }
            if(visited[e.to]) continue;
            const int nx = dfs(dfs, e.to, pval, e);
            if(nx != -1) {
                cycle.emplace_back(e);
                if(cur == nx) {
                    finish = 1;
                    return -1;
                }
                return nx;
            }
        }
        pre[cur] = -1;
        return -1;
    };
    for(int i = 0; i < n; ++i) {
        if(visited[i]) continue;
        dfs(dfs, i, i, Edge<T>());
        if(finish) {
            reverse(begin(cycle), end(cycle));
            return cycle;
        }
    }
    return {};
}
#line 5 "verify/library_checker/graph/cycle_detection_undirected.test.cpp"
int main(void) {
    int n, m;
    cin >> n >> m;
    Graph<int> g(n);
    rep(i, 0, m) {
        int a, b;
        cin >> a >> b;
        g.add_edge(a, b);
    }
    Edges<int> cycle = cycle_detection(g, false);
    if(cycle.empty()) {
        cout << -1 << '\n';
        return 0;
    }
    cout << cycle.size() << '\n';
    for(const Edge<int>& e : cycle) {
        cout << e.from << ' ';
    }
    cout << '\n';
    for(const Edge<int>& e : cycle) {
        cout << e.idx << ' ';
    }
    cout << '\n';
}
Back to top page