結果
問題 | No.1865 Make Cycle |
ユーザー |
|
提出日時 | 2022-03-04 22:00:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 188 ms / 3,000 ms |
コード長 | 1,481 bytes |
コンパイル時間 | 1,742 ms |
コンパイル使用メモリ | 174,936 KB |
実行使用メモリ | 9,568 KB |
最終ジャッジ日時 | 2024-07-18 20:25:39 |
合計ジャッジ時間 | 5,229 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 20 |
ソースコード
#include <bits/stdc++.h> using namespace std; #ifdef _RUTHEN #include "debug.hpp" #else #define show(...) true #endif vector<int> topological_sort(vector<vector<int>> &G) { int N = (int)G.size(); vector<int> deg(N, 0); // 入次数をメモ for (int i = 0; i < N; i++) { for (int &v : G[i]) deg[v]++; } // 入次数0の点の集合を作成 queue<int> que; for (int i = 0; i < N; i++) { if (deg[i] == 0) que.push(i); } vector<int> res; while (!que.empty()) { int v = que.front(); que.pop(); res.push_back(v); for (int &nx : G[v]) { // 入次数を減らす deg[nx]--; // 新たな入次数0の点を追加 if (deg[nx] == 0) que.push(nx); } } return res; } using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) template <class T> using V = vector<T>; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, Q; cin >> N >> Q; V<int> a(Q), b(Q); rep(i, Q) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } int ok = Q + 1, ng = 0; while (ok - ng > 1) { int md = (ok + ng) / 2; V<V<int>> G(N); rep(i, md) G[a[i]].push_back(b[i]); V<int> res = topological_sort(G); if (res.size() == N) { ng = md; } else { ok = md; } } cout << (ok == Q + 1 ? -1 : ok) << '\n'; return 0; }