結果

問題 No.763 Noelちゃんと木遊び
ユーザー むらためむらため
提出日時 2019-01-30 01:28:05
言語 Nim
(2.0.2)
結果
TLE  
実行時間 -
コード長 1,503 bytes
コンパイル時間 2,971 ms
コンパイル使用メモリ 70,048 KB
実行使用メモリ 31,476 KB
最終ジャッジ日時 2023-09-14 03:13:16
合計ジャッジ時間 9,371 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

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

proc findRoot(E:seq[seq[int]]) : int =
  var visited = newSeq[bool](E.len)
  proc visit(src:int) =
    visited[src] = true
    for dst in E[src]: visit(dst)
  for src in 0..<E.len:
    if not visited[src] :
      result = src
    visit(src)

let n = scan()
var E = newSeqWith(n,newSeq[int]())
(n-1).times: E[scan()-1] &= scan()-1
var preHasCutDP = newSeqWith(n,-1)
var preHasNotCutDP = newSeqWith(n,-1)
var time = 0
proc solve(preHasCut:bool,x: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]: whenCut += solve(true,e)
  var whenNotCut = 0
  for e in E[x]: whenNotCut += solve(false,e)
  if preHasCut : whenNotCut += 1
  result = max(whenCut,whenNotCut)
  if preHasCut:preHasCutDP[x] = result
  else: preHasNotCutDP[x] = result
let root = E.findRoot()
# proc getLen(x:int):int =
#   # echo x
#   for e in E[x] : result .max= getLen(e) + 1
# echo toSeq(0..100).mapIt(getLen(it))
echo solve(true,root)
# echo root.getLen()
# echo E.mapIt(it.len).max()
0