結果
| 問題 |
No.1660 Matrix Exponentiation
|
| コンテスト | |
| ユーザー |
umezo
|
| 提出日時 | 2021-08-27 22:42:17 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 58 ms / 2,000 ms |
| コード長 | 880 bytes |
| コンパイル時間 | 1,967 ms |
| コンパイル使用メモリ | 194,768 KB |
| 最終ジャッジ日時 | 2025-01-24 03:25:26 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
#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;
}
umezo