結果

問題 No.1660 Matrix Exponentiation
ユーザー kimiyukikimiyuki
提出日時 2021-08-27 22:49:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,552 bytes
コンパイル時間 2,413 ms
コンパイル使用メモリ 206,016 KB
実行使用メモリ 19,216 KB
最終ジャッジ日時 2023-08-13 10:07:37
合計ジャッジ時間 3,819 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 5 ms
5,860 KB
testcase_10 AC 5 ms
5,716 KB
testcase_11 AC 4 ms
5,636 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 29 ms
7,032 KB
testcase_20 AC 20 ms
6,428 KB
testcase_21 AC 26 ms
5,652 KB
testcase_22 AC 4 ms
4,892 KB
testcase_23 AC 14 ms
5,332 KB
testcase_24 AC 60 ms
17,776 KB
testcase_25 AC 21 ms
6,960 KB
testcase_26 AC 67 ms
17,696 KB
testcase_27 AC 27 ms
9,744 KB
testcase_28 AC 32 ms
8,560 KB
testcase_29 AC 63 ms
19,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;

int64_t solve(int n, int k, const std::vector<int> &r, const vector<int> &c) {
    vector<vector<int>> g(n);
    REP (i, k) {
        g[r[i]].push_back(c[i]);
    }

    vector<int> dp(n, -1);
    vector<bool> stk(n);
    auto go = [&](auto &&go, int y) -> bool {
        stk[y] = true;
        dp[y] = 1;
        for (int x : g[y]) {
            if (stk[x]) {
                return false;
            }
            if (dp[x] == -1) {
                if (not go(go, x)) {
                    return false;
                }
            }
            dp[y] = max(dp[y], dp[x] + 1);
        }
        stk[y] = false;
        return true;
    };
    REP (y, n) {
        if (dp[y] == -1) {
            if (not go(go, y)) {
                return -1;
            }
        }
    }
    return *max_element(ALL(dp));
}

// generated by oj-template v4.8.0 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, K;
    std::cin >> N >> K;
    std::vector<int> r(K), c(K);
    REP (i, K) { std::cin >> r[i] >> c[i]; -- r[i]; -- c[i]; }
    auto ans = solve(N, K, r, c);
    std::cout << ans << '\n';
    return 0;
}
0