結果

問題 No.1660 Matrix Exponentiation
ユーザー KudeKude
提出日時 2021-08-27 21:46:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,670 bytes
コンパイル時間 4,302 ms
コンパイル使用メモリ 262,748 KB
実行使用メモリ 9,476 KB
最終ジャッジ日時 2023-08-13 08:31:07
合計ジャッジ時間 6,024 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 2 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 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 5 ms
6,512 KB
testcase_10 AC 6 ms
6,440 KB
testcase_11 AC 4 ms
6,764 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 1 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,384 KB
testcase_19 AC 29 ms
6,888 KB
testcase_20 AC 22 ms
6,540 KB
testcase_21 AC 28 ms
4,992 KB
testcase_22 AC 5 ms
5,460 KB
testcase_23 AC 13 ms
5,720 KB
testcase_24 AC 43 ms
9,416 KB
testcase_25 AC 21 ms
7,060 KB
testcase_26 AC 40 ms
9,476 KB
testcase_27 AC 28 ms
9,012 KB
testcase_28 AC 37 ms
8,360 KB
testcase_29 AC 29 ms
9,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

std::vector<int> topological_sort(const std::vector<std::vector<int>>& graph) {
    const int n = graph.size();
    std::vector<int> indeg(n);
    for(int u = 0; u < n; u++) for(auto v: graph[u]) {
        indeg[v]++;
    }
    std::vector<int> todo; todo.reserve(n);
    for(int u = 0; u < n; u++) if (indeg[u] == 0) todo.push_back(u);
    std::vector<int> ret; ret.reserve(n);
    while(!todo.empty()) {
        int u = todo.back(); todo.pop_back();
        ret.push_back(u);
        for(auto v: graph[u]) {
            indeg[v]--;
            if (indeg[v] == 0) todo.push_back(v);
        }
    }
    if (int(ret.size()) != n) return {};
    return ret;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    VVI to(n);
    rep(_, k) {
        int r, c;
        cin >> r >> c;
        to[r - 1].push_back(c - 1);
    }
    auto ts = topological_sort(to);
    if (ts.size() == 0) {
        cout << -1 << endl;
        return 0;
    }
    VI dp(n);
    for(int i: ts) for(int j: to[i]) chmax(dp[j], dp[i] + 1);
    int ans = *max_element(all(dp)) + 1;
    cout << ans << endl;
}
0