結果
問題 | No.19 ステージの選択 |
ユーザー | ayaoni |
提出日時 | 2021-08-30 21:37:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 45 ms / 5,000 ms |
コード長 | 2,284 bytes |
コンパイル時間 | 284 ms |
コンパイル使用メモリ | 82,180 KB |
実行使用メモリ | 52,992 KB |
最終ジャッジ日時 | 2024-11-24 05:30:19 |
合計ジャッジ時間 | 3,099 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 24 |
ソースコード
import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) class SCC: def __init__(self,N): self.N = N self.graph = [[] for _ in range(N)] self.graph_rev = [[] for _ in range(N)] self.flag = [False]*N def add_edge(self,start,end): if start == end: return self.graph[start].append(end) self.graph_rev[end].append(start) def dfs(self,node,graph): self.flag[node] = True for n in graph[node]: if self.flag[n]: continue self.dfs(n,graph) self.order.append(node) def first_dfs(self): self.flag = [False]*self.N self.order = [] for i in range(self.N): if not self.flag[i]: self.dfs(i,self.graph) def second_dfs(self): self.flag = [False]*self.N self.ans = [] for n in self.order[::-1]: if self.flag[n]: continue self.flag[n] = True self.order = [] self.dfs(n,self.graph_rev) self.order.reverse() self.ans.append(self.order) def scc(self): self.first_dfs() self.second_dfs() return self.ans N = I() scc = SCC(N) a = 0 # 答えの2倍 L = [] Graph = [[] for _ in range(N)] for i in range(N): l,s = MI() a += l if s-1 != i: scc.add_edge(s-1,i) Graph[s-1].append(i) L.append(l) SCC = scc.scc() M = len(SCC) group = [-1]*N for i in range(M): for e in SCC[i]: group[e] = i in_deg = [0]*M for i in range(N): s = group[i] for j in Graph[i]: t = group[j] if s == t: continue in_deg[t] += 1 for i in range(M): if in_deg[i]: continue a += min(L[j] for j in SCC[i]) if a % 2 == 0: ans = str(a//2)+'.0' else: ans = str(a//2)+'.5' print(ans)