結果

問題 No.1660 Matrix Exponentiation
ユーザー stoqstoq
提出日時 2021-08-03 16:23:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 817 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 205,528 KB
実行使用メモリ 9,500 KB
最終ジャッジ日時 2023-08-13 07:25:40
合計ジャッジ時間 4,127 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 5 ms
6,428 KB
testcase_10 AC 4 ms
6,592 KB
testcase_11 AC 5 ms
6,404 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 59 ms
6,672 KB
testcase_20 AC 41 ms
6,456 KB
testcase_21 AC 60 ms
5,008 KB
testcase_22 AC 5 ms
5,416 KB
testcase_23 AC 25 ms
5,800 KB
testcase_24 AC 90 ms
9,500 KB
testcase_25 AC 47 ms
7,128 KB
testcase_26 AC 87 ms
9,328 KB
testcase_27 AC 63 ms
9,424 KB
testcase_28 AC 76 ms
8,444 KB
testcase_29 AC 74 ms
9,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> E(n);
  vector<int> in(n, 0);
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    E[a].push_back(b);
    in[b]++;
  }
  queue<int> que;
  vector<int> d(n, -1);
  for (int i = 0; i < n; i++) {
    if (in[i] == 0) {
      que.push(i);
      d[i] = 0;
    }
  }
  while (not que.empty()) {
    int u = que.front();
    que.pop();
    for (auto v : E[u]) {
      if (d[v] < d[u] + 1) d[v] = d[u] + 1;
      if (--in[v] == 0) {
        que.push(v);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (in[i] > 0) {
      ans = -1;
      break;
    }
    if (ans < d[i] + 1) ans = d[i] + 1;
  }
  cout << ans << "\n";
}
0