結果

問題 No.763 Noelちゃんと木遊び
ユーザー むらためむらため
提出日時 2019-01-30 01:43:29
言語 Nim
(2.0.2)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 1,156 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 66,924 KB
実行使用メモリ 21,992 KB
最終ジャッジ日時 2023-09-14 03:13:06
合計ジャッジ時間 4,602 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
21,992 KB
testcase_01 AC 17 ms
6,924 KB
testcase_02 AC 51 ms
12,700 KB
testcase_03 AC 33 ms
10,496 KB
testcase_04 AC 18 ms
7,484 KB
testcase_05 AC 32 ms
9,948 KB
testcase_06 AC 71 ms
14,800 KB
testcase_07 AC 63 ms
14,448 KB
testcase_08 AC 35 ms
10,464 KB
testcase_09 AC 18 ms
7,300 KB
testcase_10 AC 8 ms
4,788 KB
testcase_11 AC 66 ms
14,980 KB
testcase_12 AC 61 ms
13,600 KB
testcase_13 AC 57 ms
13,348 KB
testcase_14 AC 50 ms
12,388 KB
testcase_15 AC 29 ms
10,336 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 31 ms
10,412 KB
testcase_18 AC 70 ms
14,900 KB
testcase_19 AC 59 ms
13,676 KB
testcase_20 AC 63 ms
13,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils
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)
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

let n = scan()
var E = newSeqWith(n,newSeq[int]())
(n-1).times:
  let u = scan() - 1
  let v = scan() - 1
  E[u] &= v
  E[v] &= u

var preHasCutDP = newSeqWith(n,-1)
var preHasNotCutDP = newSeqWith(n,-1)
var time = 0
proc solve(preHasCut:bool,x:int,preX:int):int =
  if E[x].len == 0 : return if preHasCut: 1 else: 0
  if preHasCut and preHasCutDP[x] != -1 : return preHasCutDP[x]
  if not preHasCut and preHasNotCutDP[x] != -1 : return preHasNotCutDP[x]
  time += 1
  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
echo solve(true,0,-1)
0