結果

問題 No.763 Noelちゃんと木遊び
ユーザー むらため
提出日時 2019-01-30 02:02:20
言語 Nim
(2.2.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,371 bytes
コンパイル時間 3,486 ms
コンパイル使用メモリ 76,068 KB
実行使用メモリ 22,144 KB
最終ジャッジ日時 2025-03-22 10:35:21
合計ジャッジ時間 5,395 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(6, 1) Warning: template 'stopwatch' is implicitly redefined; this is deprecated, add an explicit .redefine pragma [ImplicitTemplateRedefinition]
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]

ソースコード

diff #

import sequtils,times,lists
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
template `min=`*(x,y) = x = min(x,y)
template stopwatch(body) = (let t1 = cpuTime();body;echo "TIME:",(cpuTime() - t1) * 1000,"ms")
template stopwatch(body) = 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
# 31634
let n = scan()
var E : array[100010,SinglyLinkedList[int]]
var preHasCutDP : array[100010,int]
var preHasNotCutDP : array[100010,int]
stopwatch:
  (n-1).times:
    let u = scan() - 1
    let v = scan() - 1
    E[u].prepend(v)
    E[v].prepend(u)
  for i in 0..<n:
    preHasCutDP[i] = -1
    preHasNotCutDP[i] = -1
  proc solve(preHasCut:bool,x:int,preX:int):int =
    if preHasCut and preHasCutDP[x] != -1 : return preHasCutDP[x]
    if not preHasCut and preHasNotCutDP[x] != -1 : return preHasNotCutDP[x]
    var whenCut = 0
    for e in E[x]:
      if e != preX : whenCut += solve(true,e,x)
    var whenNotCut = 0
    for e in E[x]:
      if e != preX : whenNotCut += solve(false,e,x)
    if preHasCut : whenNotCut += 1
    result = max(whenCut,whenNotCut)
    if preHasCut:preHasCutDP[x] = result
    else: preHasNotCutDP[x] = result
stopwatch: echo solve(true,0,-1)
0