結果

問題 No.1660 Matrix Exponentiation
ユーザー kimiyukikimiyuki
提出日時 2021-08-27 22:17:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,420 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 213,624 KB
実行使用メモリ 88,608 KB
最終ジャッジ日時 2024-05-01 02:55:39
合計ジャッジ時間 6,246 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,752 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 3 ms
6,944 KB
testcase_19 AC 107 ms
13,440 KB
testcase_20 AC 48 ms
9,728 KB
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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;

set<pair<int, int>> mult(const vector<vector<int>> &f, const set<pair<int, int>> &g) {
    set<pair<int, int>> h;
    for (auto [z, x] : g) {
        for (int y : f[z]) {
            h.emplace(y, x);
        }
    }
    return h;
}

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

    int ans = 1;
    while (not g.empty()) {
        for (auto [y, x] : g) {
            if (y == x) {
                return -1;
            }
        }
        g = mult(f, g);
        ans += 1;
    }
    return ans;
}

// 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