結果

問題 No.1865 Make Cycle
ユーザー miscalcmiscalc
提出日時 2022-03-04 21:58:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 3,000 ms
コード長 2,238 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 209,504 KB
実行使用メモリ 9,980 KB
最終ジャッジ日時 2023-09-26 00:57:50
合計ジャッジ時間 7,325 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 195 ms
8,016 KB
testcase_01 AC 124 ms
6,980 KB
testcase_02 AC 220 ms
8,300 KB
testcase_03 AC 79 ms
7,816 KB
testcase_04 AC 143 ms
9,092 KB
testcase_05 AC 46 ms
8,520 KB
testcase_06 AC 182 ms
8,060 KB
testcase_07 AC 172 ms
7,912 KB
testcase_08 AC 235 ms
9,228 KB
testcase_09 AC 42 ms
8,248 KB
testcase_10 AC 201 ms
8,828 KB
testcase_11 AC 204 ms
8,460 KB
testcase_12 AC 166 ms
8,212 KB
testcase_13 AC 177 ms
8,292 KB
testcase_14 AC 125 ms
7,320 KB
testcase_15 AC 222 ms
8,588 KB
testcase_16 AC 243 ms
8,864 KB
testcase_17 AC 43 ms
7,276 KB
testcase_18 AC 45 ms
8,632 KB
testcase_19 AC 328 ms
9,980 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/modint>
using namespace atcoder;
//using mint = modint998244353;
using mint = modint1000000007;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tlll = tuple<ll, ll, ll>;
constexpr ll INF = 1LL << 60;
template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
ll safemod(ll A, ll M) {return (A % M + M) % M;}
ll divfloor(ll A, ll B) {if (B < 0) {return divfloor(-A, -B);} return (A - safemod(A, B)) / B;}
ll divceil(ll A, ll B) {if (B < 0) {return divceil(-A, -B);} return divfloor(A + B - 1, B);}
template<class T> void unique(vector<T> &V) {V.erase(unique(V.begin(), V.end()), V.end());}
template<class T> void sortunique(vector<T> &V) {sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end());}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)
template<class T> void printvec(vector<T> &V) {int _n = V.size(); for (int i = 0; i < _n; i++) cout << V[i] << (i == _n - 1 ? "" : " ");cout << '\n';}
template<class T> void printvect(vector<T> &V) {for (auto v : V) cout << v << '\n';}
template<class T> void printvec2(vector<vector<T>> &V) {for (auto &v : V) printvec(v);}

ll N, Q;
vector<ll> A, B;
bool isok(ll k)
{
  vector<vector<ll>> G(N);
  vector<ll> H(N, 0);
  for (ll i = 0; i < k; i++)
  {
    G.at(A.at(i)).push_back(B.at(i));
    H.at(B.at(i))++;
  }

  queue<ll> que;
  for (ll i = 0; i < N; i++)
  {
    if (H.at(i) == 0)
      que.push(i);
  }

  while (!que.empty())
  {
    ll v = que.front();
    que.pop();

    for (auto nv : G.at(v))
    {
      H.at(nv)--;
      if (H.at(nv) == 0)
        que.push(nv);
    }
  }

  /*
  cerr << k << "  ";
  printvec(H);
  //*/
  return *max_element(H.begin(), H.end()) == 0;
}

int main()
{
  cin >> N >> Q;
  A.resize(Q), B.resize(Q);
  for (ll i = 0; i < Q; i++)
  {
    cin >> A.at(i) >> B.at(i);
    A.at(i)--, B.at(i)--;
  }

  if (isok(Q))
    FINALANS(-1);
  ll ok = -1, ng = Q;
  while (abs(ok - ng) > 1)
  {
    ll mid = (ok + ng) / 2;
    if (isok(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok + 1 << endl;
}
0