結果

問題 No.2464 To DAG
ユーザー Kude
提出日時 2023-09-08 21:56:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 1,556 bytes
コンパイル時間 3,502 ms
コンパイル使用メモリ 276,932 KB
実行使用メモリ 52,808 KB
最終ジャッジ日時 2024-06-26 14:55:18
合計ジャッジ時間 15,906 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  VVI to(n);
  rep(_, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    to[u].emplace_back(v);
  }
  vector<P> ans;
  vector<char> on_path(n);
  rep(i, n) {
    auto dfs = [&](auto&& self, int u) -> int {
      if (on_path[u]) {
        return u;
      }
      on_path[u] = true;
      while(to[u].size()) {
        int v = to[u].back(); to[u].pop_back();
        int res = self(self, v);
        if (res == -1) {
          ans.emplace_back(u, v);
        } else if (res != u) {
          on_path[u] = false;
          return res;
        }
      }
      on_path[u] = false;
      return -1;
    };
    dfs(dfs, i);
  }
  cout << n << ' ' << ans.size() << '\n';
  for(auto [u, v] : ans) cout << u + 1 << ' ' << v + 1 << '\n';
}
0