結果

問題 No.763 Noelちゃんと木遊び
ユーザー 👑 obakyanobakyan
提出日時 2019-06-06 00:00:55
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 837 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 5,328 KB
実行使用メモリ 18,056 KB
最終ジャッジ日時 2023-10-23 20:07:04
合計ジャッジ時間 3,775 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
16,596 KB
testcase_01 AC 46 ms
8,240 KB
testcase_02 AC 123 ms
15,444 KB
testcase_03 AC 74 ms
11,444 KB
testcase_04 AC 52 ms
8,892 KB
testcase_05 AC 76 ms
11,196 KB
testcase_06 AC 174 ms
17,792 KB
testcase_07 AC 157 ms
17,304 KB
testcase_08 AC 92 ms
11,528 KB
testcase_09 AC 51 ms
8,720 KB
testcase_10 AC 21 ms
5,124 KB
testcase_11 AC 164 ms
18,056 KB
testcase_12 AC 143 ms
16,520 KB
testcase_13 AC 139 ms
16,308 KB
testcase_14 AC 129 ms
15,032 KB
testcase_15 AC 86 ms
11,320 KB
testcase_16 AC 14 ms
4,348 KB
testcase_17 AC 78 ms
11,384 KB
testcase_18 AC 160 ms
17,872 KB
testcase_19 AC 145 ms
16,644 KB
testcase_20 AC 134 ms
16,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = {}
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  t[a][b] = true
  t[b][a] = true
end

local function getcnt(tbl)
  local c = 0
  for k, v in pairs(tbl) do c = c + 1 end
  return c
end

local tasks = {}
local tasknum, done = 0, 0
for i = 1, n do
  if getcnt(t[i]) == 1 then
    tasknum = tasknum + 1
    table.insert(tasks, i)
  end
end
local cnt = 0
while done < tasknum do
  done = done + 1
  local curidx = tasks[done]
  for dstidx, v in pairs(t[curidx]) do
    for ddidx, v2 in pairs(t[dstidx]) do
      t[dstidx][ddidx], t[ddidx][dstidx] = nil, nil
      local c = getcnt(t[ddidx])
      if c == 0 then cnt = cnt + 1
      elseif c == 1 then
        table.insert(tasks, ddidx)
        tasknum = tasknum + 1
      end
    end
    break
  end
end
print(cnt)
0