結果
問題 | No.1660 Matrix Exponentiation |
ユーザー | tko919 |
提出日時 | 2021-08-27 22:11:13 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 68 ms / 2,000 ms |
コード長 | 1,941 bytes |
コンパイル時間 | 2,505 ms |
コンパイル使用メモリ | 210,024 KB |
実行使用メモリ | 28,316 KB |
最終ジャッジ日時 | 2023-08-13 09:11:18 |
合計ジャッジ時間 | 4,720 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
19,024 KB |
testcase_01 | AC | 6 ms
18,988 KB |
testcase_02 | AC | 7 ms
19,216 KB |
testcase_03 | AC | 6 ms
19,004 KB |
testcase_04 | AC | 7 ms
19,008 KB |
testcase_05 | AC | 7 ms
19,000 KB |
testcase_06 | AC | 6 ms
19,008 KB |
testcase_07 | AC | 6 ms
19,112 KB |
testcase_08 | AC | 7 ms
19,080 KB |
testcase_09 | AC | 10 ms
21,324 KB |
testcase_10 | AC | 10 ms
21,256 KB |
testcase_11 | AC | 8 ms
19,792 KB |
testcase_12 | AC | 7 ms
19,092 KB |
testcase_13 | AC | 7 ms
19,060 KB |
testcase_14 | AC | 6 ms
19,016 KB |
testcase_15 | AC | 7 ms
19,104 KB |
testcase_16 | AC | 6 ms
19,128 KB |
testcase_17 | AC | 7 ms
19,100 KB |
testcase_18 | AC | 7 ms
19,024 KB |
testcase_19 | AC | 40 ms
21,980 KB |
testcase_20 | AC | 28 ms
21,796 KB |
testcase_21 | AC | 36 ms
20,988 KB |
testcase_22 | AC | 10 ms
20,448 KB |
testcase_23 | AC | 20 ms
20,880 KB |
testcase_24 | AC | 64 ms
27,992 KB |
testcase_25 | AC | 29 ms
21,968 KB |
testcase_26 | AC | 68 ms
27,948 KB |
testcase_27 | AC | 33 ms
22,968 KB |
testcase_28 | AC | 39 ms
22,024 KB |
testcase_29 | AC | 55 ms
28,316 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; //template #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} //end typedef pair<int,int> P; int n,m; vector<P> g[501010]; vector<int> res; int used[501010]; P par[501010]; bool dfs(int v){ used[v]=1; for(auto& e:g[v]){ par[v]={e.first,e.second}; if(used[e.first]==0 and dfs(e.first))return 1; else if(used[e.first]==1){ int cur=e.first; do{ res.push_back(par[cur].second); cur=par[cur].first; }while(cur!=e.first); return 1; } } used[v]=2; return 0; } bool cyclic(){ rep(i,0,n)used[i]=0,par[i]={-1,-1}; rep(i,0,n)if(used[i]==0 and dfs(i))return 1; return 0; } int main(){ scanf("%d%d",&n,&m); vector<int> h(n); rep(i,0,m){ int x,y; scanf("%d%d",&x,&y); x--; y--; g[x].push_back({y,i}); h[y]++; } if(cyclic()){ puts("-1"); } else{ queue<int> none; rep(i,0, n) { if (h[i] == 0) { none.push(i); } } vector<int> tsort; while (none.size()) { int i = none.front(); none.pop(); tsort.push_back(i); for (auto& [j,tmp] : g[i]) { h[j]--; if (h[j] == 0) none.push(j); } } reverse(tsort.begin(), tsort.end()); vector<int> dp(n, 0); int ans = 0; for (int i : tsort) { for(auto& [u,tmp]:g[i]){ dp[i] = max(dp[i], dp[u] + 1); } ans = max(ans, dp[i]); } cout << ans+1 << endl; return 0; } return 0; }