結果

問題 No.1660 Matrix Exponentiation
ユーザー chocoruskchocorusk
提出日時 2021-08-27 21:49:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,120 bytes
コンパイル時間 4,072 ms
コンパイル使用メモリ 194,436 KB
実行使用メモリ 26,984 KB
最終ジャッジ日時 2024-05-01 02:16:14
合計ジャッジ時間 4,719 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 9 ms
12,124 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 66 ms
13,708 KB
testcase_20 AC 45 ms
12,852 KB
testcase_21 AC 65 ms
8,064 KB
testcase_22 AC 7 ms
8,960 KB
testcase_23 AC 29 ms
10,496 KB
testcase_24 AC 98 ms
26,640 KB
testcase_25 AC 58 ms
15,904 KB
testcase_26 AC 97 ms
26,704 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 82 ms
17,480 KB
testcase_29 AC 109 ms
26,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
using mint=modint1000000007;
struct SCC{
    vector<vector<int>> g, gr;
    int n, k;
    vector<int> cmp, vs;
    vector<bool> used;
    void dfs(int x){
        used[x]=1;
        for(auto y:g[x]){
            if(!used[y]) dfs(y);
        }
        vs.push_back(x);
    }
    void rdfs(int v, int k){
        used[v]=1;
        cmp[v]=k;
        for(auto y:gr[v]){
            if(!used[y]) rdfs(y, k);
        }
    }
    SCC(const vector<vector<int>> &g):g(g), n(g.size()), cmp(n), used(n){
        gr.resize(n);
        for(int x=0; x<n; x++) for(auto y:g[x]) gr[y].push_back(x);
        for(int i=0; i<n; i++){
            if(!used[i]) dfs(i);
        }
        fill(used.begin(), used.end(), 0);
        k=0;
        for(int i=vs.size()-1; i>=0; i--){
            if(!used[vs[i]]) rdfs(vs[i], k++);
        }
    }
};
int main()
{
    int n, k; cin>>n>>k;
    if(k==0){
        cout<<1<<endl;
        return 0;
    }
    vector<vector<int>> g(n);
    for(int i=0; i<k; i++){
        int r, c; cin>>r>>c;
        r--; c--;
        if(r==c){
            cout<<-1<<endl;
            return 0;
        }
        g[r].push_back(c);
    }
    SCC scc(g);
    if(scc.k<n){
        cout<<-1<<endl;
        return 0;
    }
    int d[100010];
    fill(d, d+n, 1);
    vector<int> v(n);
    for(int i=0; i<n; i++){
        v[scc.cmp[i]]=i;
    }
    for(int i=0; i<n; i++){
        int x=v[i];
        for(auto y:g[x]){
            d[y]=max(d[y], d[x]+1);
        }
    }
    cout<<*max_element(d, d+n)<<endl;
    return 0;
}
0