結果
問題 | No.1660 Matrix Exponentiation |
ユーザー |
![]() |
提出日時 | 2022-01-20 17:41:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 103 ms / 2,000 ms |
コード長 | 2,218 bytes |
コンパイル時間 | 4,164 ms |
コンパイル使用メモリ | 265,932 KB |
最終ジャッジ日時 | 2025-01-27 13:13:48 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i<n; i++) #define repx(i, l, n) for(int i=l; i<n; i++) #define all(v) v.begin(), v.end() #define show(x) cout << #x << ": " << x << endl; #define list(x) cout << #x << ": " << x << " "; #define pb push_back using vi = vector<lint>; using vvi = vector<vector<lint>>; template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); } 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; } template<class T> inline void drop(T x) { cout << x << endl; exit(0); } template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; #define mp make_pair using plint = pair<lint, lint>; vector<vector<pair<lint, lint>>> G; vector<vector<pair<lint, lint>>> R; void make_round_graph(lint N, lint M) { lint x, y; G.resize(N), R.resize(N); rep(i, M) { cin >> x >> y; x--, y--; G[x].push_back(make_pair(1, y)); R[y].push_back(make_pair(1, x)); } } void dijkstra(vector<lint> &dist) { lint a=0, b=0, x, y, N = G.size(); dist.resize(N, -1); priority_queue<plint, vector<plint>, greater<plint>> PQ; rep(i, N) { if (R[i].size() == 0) { dist[i] = 0; PQ.push(make_pair(0, i)); } } while (!PQ.empty()) { tie(x, a) = PQ.top(); PQ.pop(); if (dist[a] > x) continue; for (auto p : G[a]) { tie(y, b) = p; if (chmax(dist[b], x+y)) PQ.push(make_pair(x+y, b)); } } } vi topo_sort() { vi res; lint N = G.size(); vi v(N); rep(i, N) { for (auto e : G[i]) v[e.second]++; } queue<int> Q; rep(i, N) { if (!v[i]) Q.push(i); } while (!Q.empty()) { int x = Q.front(); Q.pop(); res.push_back(x); for (auto e : G[x]) { v[e.second]--; if (!v[e.second]) Q.push(e.second); } } return res; } int main() { lint N, K; cin >> N >> K; make_round_graph(N, K); lint a=0, b=0, c=0, x, y, z; vi v, w; w = topo_sort(); if (w.size() < N) drop(-1); dijkstra(v); rep(i, N) { chmax(a, v[i]); } std::cout << a+1 << '\n'; }