結果

問題 No.1660 Matrix Exponentiation
ユーザー umezoumezo
提出日時 2021-08-27 22:42:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 880 bytes
コンパイル時間 2,027 ms
コンパイル使用メモリ 201,596 KB
実行使用メモリ 15,896 KB
最終ジャッジ日時 2023-08-13 09:59:03
合計ジャッジ時間 4,600 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,840 KB
testcase_01 AC 3 ms
5,728 KB
testcase_02 AC 3 ms
5,720 KB
testcase_03 AC 3 ms
5,724 KB
testcase_04 AC 4 ms
5,844 KB
testcase_05 AC 3 ms
5,728 KB
testcase_06 AC 4 ms
5,772 KB
testcase_07 AC 3 ms
5,724 KB
testcase_08 AC 4 ms
5,720 KB
testcase_09 AC 6 ms
6,388 KB
testcase_10 AC 6 ms
6,524 KB
testcase_11 AC 5 ms
6,292 KB
testcase_12 AC 4 ms
5,780 KB
testcase_13 AC 4 ms
5,776 KB
testcase_14 AC 3 ms
5,732 KB
testcase_15 AC 3 ms
5,752 KB
testcase_16 AC 3 ms
5,740 KB
testcase_17 AC 4 ms
6,020 KB
testcase_18 AC 4 ms
5,760 KB
testcase_19 AC 33 ms
7,920 KB
testcase_20 AC 23 ms
7,488 KB
testcase_21 AC 29 ms
7,172 KB
testcase_22 AC 6 ms
6,460 KB
testcase_23 AC 15 ms
7,024 KB
testcase_24 AC 58 ms
15,236 KB
testcase_25 AC 22 ms
7,156 KB
testcase_26 AC 57 ms
15,172 KB
testcase_27 AC 29 ms
9,244 KB
testcase_28 AC 41 ms
8,368 KB
testcase_29 AC 57 ms
15,896 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