結果

問題 No.763 Noelちゃんと木遊び
ユーザー むらためむらため
提出日時 2019-01-30 02:08:43
言語 Nim
(2.0.2)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,276 bytes
コンパイル時間 2,860 ms
コンパイル使用メモリ 67,176 KB
実行使用メモリ 22,112 KB
最終ジャッジ日時 2023-09-14 03:13:43
合計ジャッジ時間 4,702 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
22,112 KB
testcase_01 AC 13 ms
7,064 KB
testcase_02 AC 39 ms
12,780 KB
testcase_03 AC 25 ms
10,700 KB
testcase_04 AC 15 ms
7,596 KB
testcase_05 AC 22 ms
10,128 KB
testcase_06 AC 48 ms
15,000 KB
testcase_07 AC 47 ms
14,628 KB
testcase_08 AC 27 ms
10,640 KB
testcase_09 AC 15 ms
7,476 KB
testcase_10 AC 6 ms
4,864 KB
testcase_11 AC 50 ms
15,200 KB
testcase_12 AC 43 ms
13,752 KB
testcase_13 AC 46 ms
13,396 KB
testcase_14 AC 36 ms
12,352 KB
testcase_15 AC 23 ms
10,552 KB
testcase_16 AC 4 ms
4,388 KB
testcase_17 AC 24 ms
10,516 KB
testcase_18 AC 47 ms
15,060 KB
testcase_19 AC 46 ms
13,888 KB
testcase_20 AC 45 ms
13,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,times
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")
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)
  preHasCutDP[x] = max(whenCut,whenNotCut+1)
  preHasNotCutDP[x] = max(whenCut,whenNotCut)
  if preHasCut : whenNotCut += 1
  result = max(whenCut,whenNotCut)


echo solve(true,0,-1)
0