結果
| 問題 |
No.1660 Matrix Exponentiation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-27 22:28:09 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,338 bytes |
| コンパイル時間 | 2,439 ms |
| コンパイル使用メモリ | 205,120 KB |
| 最終ジャッジ日時 | 2025-01-24 03:20:16 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 TLE * 2 |
ソースコード
#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;
vector<int> mult(const vector<vector<int>> &f, const vector<int> &v) {
vector<int> w;
for (int y : v) {
w.insert(w.end(), ALL(f[y]));
}
sort(ALL(w));
w.erase(unique(ALL(w)), w.end());
return w;
}
int64_t solve(int n, int k, const std::vector<int> &r, const vector<int> &c) {
vector<vector<int>> f(n);
REP (i, k) {
f[r[i]].push_back(c[i]);
}
vector<int> v(n);
iota(ALL(v), 0);
int ans = 0;
while (not v.empty()) {
auto w = mult(f, v);
ans += 1;
if (v == w) return -1;
v = w;
}
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;
}