結果
問題 | No.1660 Matrix Exponentiation |
ユーザー |
|
提出日時 | 2021-08-27 22:17:41 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,420 bytes |
コンパイル時間 | 3,273 ms |
コンパイル使用メモリ | 203,456 KB |
最終ジャッジ日時 | 2025-01-24 03:14:49 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 TLE * 4 |
ソースコード
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) std::begin(x), std::end(x) using namespace std; set<pair<int, int>> mult(const vector<vector<int>> &f, const set<pair<int, int>> &g) { set<pair<int, int>> h; for (auto [z, x] : g) { for (int y : f[z]) { h.emplace(y, x); } } return h; } int64_t solve(int n, int k, const std::vector<int> &r, const std::vector<int> &c) { vector<vector<int>> f(n); set<pair<int, int>> g; REP (i, k) { f[c[i]].push_back(r[i]); g.emplace(r[i], c[i]); } int ans = 1; while (not g.empty()) { for (auto [y, x] : g) { if (y == x) { return -1; } } g = mult(f, g); ans += 1; } return ans; } // generated by oj-template v4.8.0 (https://github.com/online-judge-tools/template-generator) int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int N, K; std::cin >> N >> K; std::vector<int> r(K), c(K); REP (i, K) { std::cin >> r[i] >> c[i]; -- r[i]; -- c[i]; } auto ans = solve(N, K, r, c); std::cout << ans << '\n'; return 0; }