結果

問題 No.1660 Matrix Exponentiation
ユーザー tko919tko919
提出日時 2021-08-27 22:11:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,941 bytes
コンパイル時間 2,080 ms
コンパイル使用メモリ 211,544 KB
実行使用メモリ 29,472 KB
最終ジャッジ日時 2024-05-01 02:47:13
合計ジャッジ時間 3,604 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
17,908 KB
testcase_01 AC 6 ms
18,360 KB
testcase_02 AC 6 ms
17,344 KB
testcase_03 AC 5 ms
18,384 KB
testcase_04 AC 5 ms
17,832 KB
testcase_05 AC 5 ms
17,508 KB
testcase_06 AC 6 ms
18,184 KB
testcase_07 AC 5 ms
17,224 KB
testcase_08 AC 6 ms
17,480 KB
testcase_09 AC 10 ms
19,628 KB
testcase_10 AC 9 ms
20,448 KB
testcase_11 AC 6 ms
18,904 KB
testcase_12 AC 5 ms
17,220 KB
testcase_13 AC 6 ms
18,848 KB
testcase_14 AC 5 ms
17,476 KB
testcase_15 AC 5 ms
17,348 KB
testcase_16 AC 6 ms
17,760 KB
testcase_17 AC 5 ms
17,660 KB
testcase_18 AC 6 ms
18,880 KB
testcase_19 AC 35 ms
21,176 KB
testcase_20 AC 26 ms
20,420 KB
testcase_21 AC 35 ms
22,968 KB
testcase_22 AC 7 ms
19,256 KB
testcase_23 AC 18 ms
19,272 KB
testcase_24 AC 53 ms
29,472 KB
testcase_25 AC 35 ms
23,464 KB
testcase_26 AC 52 ms
27,968 KB
testcase_27 AC 31 ms
21,668 KB
testcase_28 AC 36 ms
20,856 KB
testcase_29 AC 51 ms
29,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0