結果

問題 No.1660 Matrix Exponentiation
ユーザー 👑 kmjpkmjp
提出日時 2021-08-27 23:40:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 2,161 bytes
コンパイル時間 2,348 ms
コンパイル使用メモリ 214,672 KB
実行使用メモリ 121,652 KB
最終ジャッジ日時 2024-05-01 04:27:11
合計ジャッジ時間 5,165 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
103,608 KB
testcase_01 AC 35 ms
104,072 KB
testcase_02 AC 36 ms
104,332 KB
testcase_03 AC 35 ms
103,424 KB
testcase_04 AC 36 ms
103,668 KB
testcase_05 AC 35 ms
105,840 KB
testcase_06 AC 34 ms
104,244 KB
testcase_07 AC 36 ms
105,132 KB
testcase_08 AC 36 ms
104,728 KB
testcase_09 AC 48 ms
109,520 KB
testcase_10 AC 47 ms
110,404 KB
testcase_11 AC 37 ms
103,712 KB
testcase_12 AC 36 ms
105,856 KB
testcase_13 AC 36 ms
103,376 KB
testcase_14 AC 36 ms
105,052 KB
testcase_15 AC 35 ms
103,824 KB
testcase_16 AC 35 ms
104,240 KB
testcase_17 AC 35 ms
104,936 KB
testcase_18 AC 35 ms
103,384 KB
testcase_19 AC 98 ms
112,008 KB
testcase_20 AC 84 ms
113,648 KB
testcase_21 AC 78 ms
109,220 KB
testcase_22 AC 45 ms
108,496 KB
testcase_23 AC 64 ms
109,100 KB
testcase_24 AC 168 ms
121,652 KB
testcase_25 AC 83 ms
114,620 KB
testcase_26 AC 169 ms
121,380 KB
testcase_27 AC 35 ms
103,488 KB
testcase_28 AC 122 ms
116,412 KB
testcase_29 AC 163 ms
120,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,K;
vector<int> E[101010];

class SCC {
public:
	static const int MV = 2025000;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<NV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0,i; SC.clear(); SC.resize(NV); NUM.clear();
		assert(NV);
		FOR(i,NV) vis[i]=0; FOR(i,NV) if(!vis[i]) dfs(i); FOR(i,NV) vis[i]=0;
		for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};
SCC scc;
int dp[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	scc.init(N);
	FOR(i,K) {
		cin>>x>>y;
		if(x==y) {
			cout<<-1<<endl;
			return;
		}
		E[x-1].push_back(y-1);
		scc.add_edge(x-1,y-1);
	}
	scc.scc();
	if(scc.SC.size()!=N) {
		cout<<-1<<endl;
		return;
	}
	int ma=0;
	FOR(i,N) {
		FORR(cur,scc.SC[i]) {
			ma=max(ma,dp[cur]+1);
			FORR(e,E[cur]) dp[e]=max(dp[e],dp[cur]+1);
		}
	}
	cout<<ma<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0