結果
問題 | No.1660 Matrix Exponentiation |
ユーザー | milanis48663220 |
提出日時 | 2021-08-27 22:19:41 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 61 ms / 2,000 ms |
コード長 | 1,935 bytes |
コンパイル時間 | 1,306 ms |
コンパイル使用メモリ | 127,892 KB |
実行使用メモリ | 21,392 KB |
最終ジャッジ日時 | 2024-05-01 02:58:43 |
合計ジャッジ時間 | 2,737 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 8 ms
8,764 KB |
testcase_10 | AC | 7 ms
8,768 KB |
testcase_11 | AC | 4 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,940 KB |
testcase_14 | AC | 1 ms
6,940 KB |
testcase_15 | AC | 2 ms
6,944 KB |
testcase_16 | AC | 2 ms
6,944 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 2 ms
6,940 KB |
testcase_19 | AC | 36 ms
10,016 KB |
testcase_20 | AC | 27 ms
9,440 KB |
testcase_21 | AC | 30 ms
6,940 KB |
testcase_22 | AC | 6 ms
6,940 KB |
testcase_23 | AC | 17 ms
7,808 KB |
testcase_24 | AC | 61 ms
20,288 KB |
testcase_25 | AC | 25 ms
9,556 KB |
testcase_26 | AC | 58 ms
20,348 KB |
testcase_27 | AC | 3 ms
6,940 KB |
testcase_28 | AC | 42 ms
12,852 KB |
testcase_29 | AC | 60 ms
21,392 KB |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #include <tuple> #include <cmath> #include <numeric> #include <functional> #include <cassert> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; bool topological_sort(vector<vector<int>> g, vector<int> &order){ int n = g.size(); order.clear(); vector<bool> used(n, false); function<void(int)> dfs = [&](int v){ used[v] = true; for(int to : g[v]){ if(!used[to]) dfs(to); } order.push_back(v); }; for(int v = 0; v < n; v++){ if(!used[v]) dfs(v); } reverse(order.begin(), order.end()); vector<int> inv_order(n); for(int i = 0; i < n; i++) inv_order[order[i]] = i; for(int v = 0; v < n; v++){ for(int u : g[v]){ if(inv_order[v] > inv_order[u]) return false; } } return true; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; ll n, k; cin >> n >> k; vector<vector<int>> g(n); for(int i = 0; i < k; i++){ int a, b; cin >> a >> b; a--; b--; if(a == b){ cout << -1 << endl; return 0; } g[a].push_back(b); } vector<int> ord; if(topological_sort(g, ord)){ vector<int> dp(n, 1); for(int v: ord){ for(int to: g[v]) chmax(dp[to], dp[v]+1); } cout << *max_element(dp.begin(), dp.end()) << endl; }else{ cout << -1 << endl; } }