結果

問題 No.1660 Matrix Exponentiation
ユーザー 沙耶花沙耶花
提出日時 2021-08-27 22:01:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 888 bytes
コンパイル時間 4,907 ms
コンパイル使用メモリ 266,160 KB
実行使用メモリ 29,792 KB
最終ジャッジ日時 2024-05-01 02:35:14
合計ジャッジ時間 6,534 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 14 ms
11,820 KB
testcase_10 AC 16 ms
12,140 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 72 ms
11,980 KB
testcase_20 AC 49 ms
11,292 KB
testcase_21 AC 66 ms
7,868 KB
testcase_22 AC 12 ms
9,044 KB
testcase_23 AC 32 ms
9,216 KB
testcase_24 AC 109 ms
29,792 KB
testcase_25 AC 64 ms
14,516 KB
testcase_26 AC 103 ms
29,660 KB
testcase_27 AC 59 ms
5,376 KB
testcase_28 AC 75 ms
11,228 KB
testcase_29 AC 82 ms
23,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001


int main(){
	
	int N,K;
	cin>>N>>K;
	
	scc_graph S(N);
	vector<int> x(K),y(K);
	bool f = false;
	rep(i,K){
		
		cin>>x[i]>>y[i];
		x[i]--;y[i]--;
		if(x[i]==y[i])f = true;
		S.add_edge(x[i],y[i]);
	}
	
	if(f){
		cout<<-1<<endl;
		return 0;
	}
	
	auto ret = S.scc();
	if(ret.size()!=N){
		cout<<-1<<endl;
		return 0;
	}
	
	vector<int> pos(N);
	rep(i,ret.size()){
		pos[ret[i][0]] = i;
	}
	
	vector<vector<int>> E(N);
	rep(i,K){
		E[pos[x[i]]].push_back(pos[y[i]]);
	}
	
	vector<int> dp(N,0);
	
	rep(i,N){
		rep(j,E[i].size()){
			int to = E[i][j];
			dp[to] = max(dp[to],dp[i]+1);
		}
	}
	
	int ans = 0;
	rep(i,N)ans = max(ans,dp[i]);
	
	cout<<ans+1<<endl;
	
	
	return 0;
}
0