結果
問題 | No.1660 Matrix Exponentiation |
ユーザー |
![]() |
提出日時 | 2022-03-30 11:58:06 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 678 ms / 2,000 ms |
コード長 | 1,365 bytes |
コンパイル時間 | 368 ms |
コンパイル使用メモリ | 82,368 KB |
実行使用メモリ | 247,948 KB |
最終ジャッジ日時 | 2024-11-14 10:59:22 |
合計ジャッジ時間 | 6,891 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
import sys sys.setrecursionlimit(10**6) def scc(N, G, RG): order = [] used = [0]*N group = [None]*N def dfs(s): used[s] = 1 for t in G[s]: if not used[t]: dfs(t) order.append(s) def rdfs(s, col): group[s] = col used[s] = 1 for t in RG[s]: if not used[t]: rdfs(t, col) for i in range(N): if not used[i]: dfs(i) used = [0]*N label = 0 for s in reversed(order): if not used[s]: rdfs(s, label) label += 1 return label, group def construct(N, G, label, group): G0 = [set() for i in range(label)] GP = [[] for i in range(label)] for v in range(N): lbs = group[v] for w in G[v]: lbt = group[w] if lbs == lbt: continue G0[lbs].add(lbt) GP[lbs].append(v) return G0, GP n,m=map(int,input().split()) edge=[[] for i in range(n)] redge=[[] for i in range(n)] for _ in range(m): a,b=map(lambda x:int(x)-1,input().split()) if a==b: print(-1) exit() edge[a].append(b) redge[b].append(a) label,group=scc(n,edge,redge) if label!=n: print(-1) exit() dp=[1]*n G0,GP=construct(n,edge,n,group) for i in range(n): for j in G0[i]: dp[j]=max(dp[j],dp[i]+1) print(max(dp))