結果
問題 |
No.330 Eigenvalue Decomposition
|
ユーザー |
|
提出日時 | 2022-07-07 15:06:03 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,055 ms / 5,000 ms |
コード長 | 2,345 bytes |
コンパイル時間 | 217 ms |
コンパイル使用メモリ | 82,300 KB |
実行使用メモリ | 228,008 KB |
最終ジャッジ日時 | 2024-12-24 12:54:24 |
合計ジャッジ時間 | 8,733 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 31 |
ソースコード
#!/usr/bin/python # -*- coding: utf-8 -*- # import from sys import stdin, setrecursionlimit setrecursionlimit(10**8) def int_map(): return map(int,input().split()) def int_list(): return list(map(int,input().split())) def int_mtx(N): x = [list(map(int, stdin.readline().split())) for _ in range(N)] return x def str_mtx(N): x = [list(stdin.readline()[:-1]) for _ in range(N)] return x def print_space(l): return print(" ".join([str(x) for x in l])) """ main code """ N,M = int_map() def scc(N, G, RG): # 一回目のDFS (順方向のグラフ G を用いる) # DFSにおいてスタックから除かれる頂点の順番 order を記録することが目的 def dfs(s): used[s] = 1 for t in G[s]: if not used[t]: dfs(t) order.append(s) # 二回目のDFS (逆方向のグラフ RG を用いる) # 上で得た order を逆順にたどって,行き詰まるまでが一つのグループ扱いになる def rdfs(s, label): group[s] = label used[s] = 1 for t in RG[s]: if not used[t]: rdfs(t, label) order = [] used = [0]*N # 一回目のDFS for i in range(N): if not used[i]: dfs(i) used = [0]*N label = 0 group = [None]*N # 二回目のDFS 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) for i in range(label): G0[i] = list(G0[i]) return G0, GP UV = [] for _ in range(M): a,b,c = int_map() UV.append([a-1,b-1]) UV.append([b-1,a-1]) G = [[] for _ in range(N)] RG = [[] for _ in range(N)] for uvi in UV: G[uvi[0]].append(uvi[1]) RG[uvi[1]].append(uvi[0]) #### 必ず有向化!!!! #### import pypyjit pypyjit.set_param('max_unroll_recursion=-1') label_num, group = scc(N, G, RG) print(label_num)