結果

問題 No.1660 Matrix Exponentiation
ユーザー 沙耶花沙耶花
提出日時 2021-08-27 22:01:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 888 bytes
コンパイル時間 4,383 ms
コンパイル使用メモリ 264,076 KB
実行使用メモリ 29,744 KB
最終ジャッジ日時 2023-08-13 08:59:03
合計ジャッジ時間 6,411 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 15 ms
11,592 KB
testcase_10 AC 15 ms
12,312 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 66 ms
11,840 KB
testcase_20 AC 46 ms
11,160 KB
testcase_21 AC 62 ms
7,732 KB
testcase_22 AC 12 ms
8,692 KB
testcase_23 AC 30 ms
9,472 KB
testcase_24 AC 100 ms
29,744 KB
testcase_25 AC 60 ms
14,348 KB
testcase_26 AC 100 ms
29,600 KB
testcase_27 AC 55 ms
5,204 KB
testcase_28 AC 71 ms
11,288 KB
testcase_29 AC 75 ms
23,128 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