結果
問題 | No.1865 Make Cycle |
ユーザー |
![]() |
提出日時 | 2022-03-04 22:03:09 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 336 ms / 3,000 ms |
コード長 | 1,611 bytes |
コンパイル時間 | 5,004 ms |
コンパイル使用メモリ | 255,492 KB |
最終ジャッジ日時 | 2025-01-28 05:19:53 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 20 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/all>using namespace std;using namespace atcoder;typedef int64_t lint;#define rep(i, n) for(int i=0; i<n; i++)#define repx(i, l, n) for(int i=l; i<n; i++)#define all(v) v.begin(), v.end()#define show(x) cout << #x << ": " << x << endl;#define list(x) cout << #x << ": " << x << " ";#define pb push_backusing vi = vector<lint>;using vvi = vector<vector<lint>>;template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); }template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }template<class T> inline void drop(T x) { cout << x << endl; exit(0); }template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; }constexpr lint LINF = LLONG_MAX/2;vi A, B;vi topo_sort(int N, int M) {vector<vector<int>> G(N);int x, y;rep(i, M) {x = A[i], y = B[i];G[x].push_back(y);}vi res;vi v(N);rep(i, N) { for (auto e : G[i]) v[e]++; }queue<int> Q;rep(i, N) { if (!v[i]) Q.push(i); }while (!Q.empty()) {int x = Q.front(); Q.pop();res.push_back(x);for (auto e : G[x]) {v[e]--;if (!v[e]) Q.push(e);}}return res;}int main() {lint N, Q;cin >> N >> Q;lint a=0, b=Q+1, c=0, x, y, z;rep(i, Q) {cin >> x >> y;A.pb(x-1), B.pb(y-1);}while (b-a > 1) {c = (a+b)/2;vi v = topo_sort(N, c);if (v.size() == N) a = c;else b = c;}if (b == Q+1) std::cout << -1 << '\n';else std::cout << b << '\n';}