結果
| 問題 |
No.19 ステージの選択
|
| ユーザー |
toyuzuko
|
| 提出日時 | 2020-05-30 14:02:48 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 32 ms / 5,000 ms |
| コード長 | 2,435 bytes |
| コンパイル時間 | 119 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 11,008 KB |
| 最終ジャッジ日時 | 2024-11-07 15:54:32 |
| 合計ジャッジ時間 | 1,969 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
class Graph(): #directed
def __init__(self, n, edge, indexed=1):
self.n = n
self.graph = [[] for _ in range(n)]
self.rev = [[] for _ in range(n)]
self.deg = [0 for _ in range(n)]
for e in edge:
self.graph[e[0] - indexed].append(e[1] - indexed)
self.rev[e[1] - indexed].append(e[0] - indexed)
self.deg[e[1] - indexed] += 1
def strongry_connected(self):
group = [None for _ in range(self.n)]
used = [0 for _ in range(self.n)]
order = []
for s in range(self.n):
if not used[s]:
stack = [s]
used[s] = 1
while stack:
node = stack.pop()
movable = False
for adj in self.graph[node]:
if not used[adj]:
movable = True
used[adj] = 1
stack.append(node)
stack.append(adj)
break
if not movable:
order.append(node)
used = [0 for _ in range(self.n)]
count = 0
for s in order[::-1]:
if not used[s]:
stack = [s]
group[s] = count
while stack:
node = stack.pop()
used[node] = 1
for adj in self.rev[node]:
if not used[adj]:
group[adj] = count
stack.append(adj)
count += 1
return group, count
import sys
input = sys.stdin.readline
N = int(input())
L = []
E = []
for i in range(N):
l, s = map(int, input().split())
L.append(l)
E.append((s, i + 1))
g = Graph(N, E)
group, count = g.strongry_connected()
group_to_node = [[] for _ in range(count)]
for i in range(N):
group_to_node[group[i]].append(i)
new_edge = set()
for a, b in E:
if group[a - 1] == group[b - 1]:
continue
new_edge.add((group[a - 1], group[b - 1]))
compressed_graph = Graph(count, new_edge, 0)
res = 0
for i in range(count):
difficulty = []
for j in group_to_node[i]:
difficulty.append(L[j])
if compressed_graph.deg[i] == 0:
res += (sum(difficulty) + min(difficulty)) / 2
else:
res += sum(difficulty) / 2
print(res)
toyuzuko