結果

問題 No.1660 Matrix Exponentiation
ユーザー kyawa
提出日時 2022-08-19 20:38:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 782 bytes
コンパイル時間 2,159 ms
コンパイル使用メモリ 203,212 KB
最終ジャッジ日時 2025-01-31 00:32:02
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    int64_t res = 0;
    int64_t N, K; cin >> N >> K;
    if(K == 0){
        cout << 1 << '\n';
        return 0;
    }
    vector<vector<int64_t>> edge(N);
    vector<int64_t> in_count(N, 0);
    while(K--){
        int64_t r, c; cin >> r >> c; r--; c--;
        edge[c].push_back(r);
        in_count[r]++;
    }
    queue<pair<int64_t,int64_t>> que;
    for(int64_t i = 0; i < N; i++) if(in_count[i] == 0) que.push({i, 0});
    while(not que.empty()){
        auto [vis, c] = que.front(); que.pop();
        res = max(res, c);
        for(auto nxt : edge[vis]) if(--in_count[nxt] == 0) que.push({nxt, c+1});
    }
    cout << (none_of(in_count.begin(), in_count.end(), [](auto x){return x > 0;}) ? res+1 : -1) << '\n';
}
0