結果

問題 No.1660 Matrix Exponentiation
ユーザー 沙耶花
提出日時 2021-08-27 22:01:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 888 bytes
コンパイル時間 4,477 ms
コンパイル使用メモリ 256,036 KB
最終ジャッジ日時 2025-01-24 03:02:21
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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