結果

問題 No.1865 Make Cycle
ユーザー simansiman
提出日時 2022-03-05 02:55:52
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,033 bytes
コンパイル時間 2,744 ms
コンパイル使用メモリ 106,748 KB
実行使用メモリ 15,688 KB
最終ジャッジ日時 2023-09-26 06:44:28
合計ジャッジ時間 13,243 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

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