結果

問題 No.1660 Matrix Exponentiation
ユーザー Kude
提出日時 2021-08-27 21:46:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,670 bytes
コンパイル時間 4,282 ms
コンパイル使用メモリ 253,632 KB
最終ジャッジ日時 2025-01-24 02:48:09
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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