結果

問題 No.1660 Matrix Exponentiation
ユーザー chocoruskchocorusk
提出日時 2021-08-27 21:49:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 2,120 bytes
コンパイル時間 3,742 ms
コンパイル使用メモリ 192,996 KB
実行使用メモリ 26,848 KB
最終ジャッジ日時 2023-08-13 08:37:43
合計ジャッジ時間 5,734 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 11 ms
11,884 KB
testcase_11 AC 2 ms
5,292 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 80 ms
13,480 KB
testcase_20 AC 56 ms
12,800 KB
testcase_21 AC 70 ms
8,076 KB
testcase_22 AC 8 ms
8,736 KB
testcase_23 AC 35 ms
10,364 KB
testcase_24 AC 134 ms
26,524 KB
testcase_25 AC 67 ms
15,884 KB
testcase_26 AC 139 ms
26,540 KB
testcase_27 AC 3 ms
5,580 KB
testcase_28 AC 111 ms
17,092 KB
testcase_29 AC 154 ms
26,848 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