結果
| 問題 |
No.1660 Matrix Exponentiation
|
| コンテスト | |
| ユーザー |
tko919
|
| 提出日時 | 2021-08-27 22:11:13 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 51 ms / 2,000 ms |
| コード長 | 1,941 bytes |
| コンパイル時間 | 2,271 ms |
| コンパイル使用メモリ | 203,864 KB |
| 最終ジャッジ日時 | 2025-01-24 03:11:56 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:40:9: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
40 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:43:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
43 | int x,y; scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end
typedef pair<int,int> P;
int n,m; vector<P> g[501010];
vector<int> res;
int used[501010]; P par[501010];
bool dfs(int v){
used[v]=1;
for(auto& e:g[v]){
par[v]={e.first,e.second};
if(used[e.first]==0 and dfs(e.first))return 1;
else if(used[e.first]==1){
int cur=e.first;
do{
res.push_back(par[cur].second);
cur=par[cur].first;
}while(cur!=e.first);
return 1;
}
} used[v]=2; return 0;
}
bool cyclic(){
rep(i,0,n)used[i]=0,par[i]={-1,-1};
rep(i,0,n)if(used[i]==0 and dfs(i))return 1;
return 0;
}
int main(){
scanf("%d%d",&n,&m);
vector<int> h(n);
rep(i,0,m){
int x,y; scanf("%d%d",&x,&y);
x--; y--;
g[x].push_back({y,i});
h[y]++;
}
if(cyclic()){
puts("-1");
}
else{
queue<int> none;
rep(i,0, n) {
if (h[i] == 0) {
none.push(i);
}
}
vector<int> tsort;
while (none.size()) {
int i = none.front(); none.pop();
tsort.push_back(i);
for (auto& [j,tmp] : g[i]) {
h[j]--;
if (h[j] == 0) none.push(j);
}
}
reverse(tsort.begin(), tsort.end());
vector<int> dp(n, 0);
int ans = 0;
for (int i : tsort) {
for(auto& [u,tmp]:g[i]){
dp[i] = max(dp[i], dp[u] + 1);
}
ans = max(ans, dp[i]);
}
cout << ans+1 << endl;
return 0;
}
return 0;
}
tko919