結果

問題 No.1660 Matrix Exponentiation
ユーザー umezoumezo
提出日時 2021-08-27 22:42:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 880 bytes
コンパイル時間 2,186 ms
コンパイル使用メモリ 203,684 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2024-05-01 03:27:16
合計ジャッジ時間 3,313 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 5 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 30 ms
7,956 KB
testcase_20 AC 20 ms
7,688 KB
testcase_21 AC 26 ms
7,876 KB
testcase_22 AC 5 ms
6,944 KB
testcase_23 AC 13 ms
7,036 KB
testcase_24 AC 41 ms
15,332 KB
testcase_25 AC 22 ms
7,336 KB
testcase_26 AC 45 ms
15,260 KB
testcase_27 AC 28 ms
9,280 KB
testcase_28 AC 37 ms
8,536 KB
testcase_29 AC 44 ms
16,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

vector<int> G[100100],B;

int color[100010];
bool flag=true;

void dfs(int u){
  color[u]=1;
  for(auto x:G[u]){
    if(color[x]==1){
      flag=false;
      return;
    }
    if(color[x]==0) dfs(x);
  }
  color[u]=2;
  B.push_back(u);
}

int dp[100010];
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n,k;
  cin>>n>>k;
  rep(i,k){
    int r,c;
    cin>>r>>c;
    r--,c--;
    G[r].push_back(c);
  }
  
  rep(i,n){
    if(color[i]==0) dfs(i);
  }
  if(flag==false){
    cout<<-1<<endl;
    return 0;
  }
  reverse(ALL(B));
  
  rep(i,n){
    int t=B[i];
    for(auto x:G[t]){
      dp[x]=max(dp[x],dp[t]+1);
    }
  }
  
  int ans=0;
  rep(i,n) ans=max(ans,dp[i]);
  cout<<ans+1<<endl;

  return 0;
}
0