結果
| 問題 | No.1660 Matrix Exponentiation |
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2021-08-27 22:19:41 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 62 ms / 2,000 ms |
| コード長 | 1,935 bytes |
| コンパイル時間 | 1,512 ms |
| コンパイル使用メモリ | 124,472 KB |
| 最終ジャッジ日時 | 2025-01-24 03:15:42 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
#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;
}
}
milanis48663220