cpp-library

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

View the Project on GitHub 9tc/cpp-library

:x: verify/LC-CycleDetection(Directed).test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/cycle_detection"

#include "../template/template.hpp"
#include "../graph/cycle-detection.hpp"

int main(){
  int N, M;
  cin >> N >> M;
  Graph G(N);
  map<pair<int,int>, int> edges;
  REP(i,M){
    int u, v;
    cin >> u >> v;
    G[u].PB(v);
    edges[{u, v}] = i;
  }

  auto cycle = detectCycle(G);
  if(cycle.size()){
    cout << cycle.size() << endl;
    REP(i,cycle.size()){
      cout << edges[{cycle[i], i+1 == cycle.size() ? cycle[0] : cycle[i+1]}] << endl;
    }
  }else{
    cout << -1 << endl;
  }
}
#line 1 "verify/LC-CycleDetection(Directed).test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/cycle_detection"

#line 1 "template/template.hpp"
#include<bits/stdc++.h>
using ll = long long;
#define REP(i, n) for(ll i = 0; (i) < ll(n); ++ (i))
#define FOR(i, m, n) for(ll i = (m); (i) <= ll(n); ++ (i))
#define REPR(i, n) for(ll i = ll(n) - 1; (i) >= 0; -- (i))
#define FORR(i, m, n) for(ll i = ll(n); (i) >= ll(m); -- (i))
#define ALL(x) x.begin(),x.end()

#define INF (int)1e9
#define LLINF (long long)1e18
#define MOD (int)(1e9+7)
#define MOD9 (int)998244353
#define PI 3.141592653589
#define PB push_back
#define F first
#define S second

#define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define Yes(T) {cout<<"Yes"<<endl; if(T) return 0;}
#define No(T) {cout <<"No"<<endl; if(T) return 0;}
#define YES(T) {cout<<"YES"<<endl; if(T) return 0;}
#define NO(T) {cout <<"NO"<<endl; if(T) return 0;}

#define Graph vector<vector<int> >
#define CostGraph vector<vector<pair<int,ll> > >
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define VI vector<int>
#define VL vector<ll>
#define VVI vector<vector<int> >
#define VVL vector<vector<ll> >
#define VPII vector<pair<int,int> >
#define VPLL vector<pair<ll,ll> >

#define DDD fixed<<setprecision(10)
#define PAD setfill('0')<<right<<setw(8)

template <class T>
inline bool chmin(T &a, T b) {
  if(a > b){ a = b; return true;}
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if(a < b){a = b; return true;}
  return false;
}
struct input{
  int n;
  input() {}
  input(int n_) : n(n_){};
  template <class T>
  operator T(){
    T ret;
    std::cin >> ret;
    return ret;
  }
  template <class T>
  operator std::vector<T>() {
    std::vector<T> ret(n);
    REP(i,n) std::cin >> ret[i];
    return ret;
  }
};
template <class T>
inline void printVec(std::vector<T> v){
  REP(i,v.size()){
    if(i) std::cout << " ";
    std::cout << v[i];
  } std::cout << std::endl;
}

using namespace std;
#line 1 "graph/cycle-detection.hpp"
vector<int> detectCycle(Graph &g){
  int n = g.size();
  const int UNUSED = 0;
  const int USING = 1;
  const int USED = 2;
  vector<int> status(n, UNUSED);
  vector<int> pre(n);
  vector<int> cycle;

  function<bool(int)> dfs = [&](int u) -> bool{
    status[u] = USING;
    for(auto &v: g[u]){
      if(status[v] == UNUSED){
        pre[v] = u;
        if(dfs(v)) return true;
      }else if(status[v] == USING){
        int cur = u;
        while(cur != v){
          cycle.push_back(pre[cur]);
          cur = pre[cur];
        }
        cycle.push_back(u);
        return true;
      }
    }
    status[u] = USED;
    return false;
  };

  for(int i = 0; i < g.size(); ++i){
    if(status[i] == UNUSED && dfs(i)){
      reverse(cycle.begin(), cycle.end());
      return cycle;
    }
  }
  return {};
};
#line 5 "verify/LC-CycleDetection(Directed).test.cpp"

int main(){
  int N, M;
  cin >> N >> M;
  Graph G(N);
  map<pair<int,int>, int> edges;
  REP(i,M){
    int u, v;
    cin >> u >> v;
    G[u].PB(v);
    edges[{u, v}] = i;
  }

  auto cycle = detectCycle(G);
  if(cycle.size()){
    cout << cycle.size() << endl;
    REP(i,cycle.size()){
      cout << edges[{cycle[i], i+1 == cycle.size() ? cycle[0] : cycle[i+1]}] << endl;
    }
  }else{
    cout << -1 << endl;
  }
}
Back to top page