結果

問題 No.1865 Make Cycle
ユーザー siman
提出日時 2022-03-05 02:55:52
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,033 bytes
コンパイル時間 2,604 ms
コンパイル使用メモリ 141,732 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2024-07-19 02:04:14
合計ジャッジ時間 11,701 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other TLE * 1 -- * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int N, Q;
int A[100010];
int B[100010];
map<int, map<int, bool>> E;
map<int, map<int, bool>> RE;

bool has_cycle(int x) {
  vector<int> cost(N + 1, 9999);

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < x; ++j) {
      int a = A[j];
      int b = B[j];

      if (cost[b] > cost[a] - 1) {
        cost[b] = cost[a] - 1;

        if (i == N - 1) {
          return true;
        }
      }
    }
  }

  return false;
}

int main() {
  cin >> N >> Q;

  for (int i = 0; i < Q; ++i) {
    cin >> A[i] >> B[i];
  }

  if (not has_cycle(Q)) {
    cout << -1 << endl;
    return 0;
  }

  int ok = Q;
  int ng = 0;

  while (abs(ok - ng) >= 2) {
    int x = (ok + ng) / 2;

    if (has_cycle(x)) {
      ok = x;
    } else {
      ng = x;
    }
  }

  cout << ok << endl;

  return 0;
}
0