結果

問題 No.679 不思議マーケット
ユーザー むらためむらため
提出日時 2019-01-28 01:03:53
言語 Nim
(2.0.2)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,216 bytes
コンパイル時間 2,525 ms
コンパイル使用メモリ 69,912 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 03:06:38
合計ジャッジ時間 3,659 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils
template times*(n:int,body) = (for _ in 0..<n: body)

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': break
    result = 10 * result + k.ord - '0'.ord

proc topologicalSort(E:seq[seq[int]],deleteIsolated:bool = false) : seq[int] =
  # E : [n->[m1,m2,m3], ... ]の隣接リスト を ソート
  var visited = newSeq[int](E.len)
  var tsorted = newSeq[int]()
  proc visit(node:int) =
    visited[node] += 1
    if visited[node] > 1: return
    for edge in E[node]: visit(edge)
    tsorted &= node # 葉から順に追加される
  for n in 0..<E.len: visit(n)
  if deleteIsolated: # 孤立点の除去
    result = tsorted.filterIt(visited[it] > 1 or E[it].len > 0)
  else: result = tsorted


let n = scan()
let m = scan()
var E = newSeqWith(n+1,newSeq[int]())
var IE = newSeqWith(n+1,newSeq[int]())
m.times:
  let g = scan()
  let r = scan()
  r.times:
    let h = scan()
    E[g] &= h
    IE[h] &= g
var bought = newSeq[bool](n+1)
var ans = 0
for e in E.topologicalSort():
  if e == 0 : continue
  if E[e].len == 0 or E[e].allIt(bought[it]):
    ans += 1
    bought[e] = true
echo ans
0