結果

問題 No.1817 Reversed Edges
ユーザー 👑 obakyanobakyan
提出日時 2022-01-22 15:29:24
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 1,475 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 5,300 KB
実行使用メモリ 25,048 KB
最終ジャッジ日時 2023-08-18 08:54:04
合計ジャッジ時間 6,343 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 231 ms
20,996 KB
testcase_08 AC 97 ms
11,328 KB
testcase_09 AC 198 ms
20,200 KB
testcase_10 AC 113 ms
12,016 KB
testcase_11 AC 142 ms
13,608 KB
testcase_12 AC 279 ms
22,308 KB
testcase_13 AC 247 ms
22,404 KB
testcase_14 AC 265 ms
22,336 KB
testcase_15 AC 265 ms
22,204 KB
testcase_16 AC 265 ms
22,444 KB
testcase_17 AC 250 ms
22,312 KB
testcase_18 AC 259 ms
22,280 KB
testcase_19 AC 257 ms
22,264 KB
testcase_20 AC 245 ms
22,388 KB
testcase_21 AC 264 ms
22,312 KB
testcase_22 AC 144 ms
20,388 KB
testcase_23 AC 141 ms
20,240 KB
testcase_24 AC 176 ms
25,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n = io.read("*n")
local a, b = {}, {}
local edge = {}
local parent = {}
local ccnt, w, score = {}, {}, {}
for i = 1, n do
  edge[i] = {}
  parent[i] = 0
  ccnt[i] = 0
  w[i] = 1
  score[i] = 0
end
for i = 1, n - 1 do
  a[i], b[i] = io.read("*n", "*n")
  table.insert(edge[a[i]], i)
  table.insert(edge[b[i]], i)
end

local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local ej = edge[src][j]
    local dst = a[ej] + b[ej] - src
    if parent[src] ~= dst then
      table.insert(tasks, dst)
      parent[dst] = src
      ccnt[src] = ccnt[src] + 1
    end
  end
end
local t2 = {}
for i = 1, n do
  if ccnt[i] == 0 then
    table.insert(t2, i)
  end
end
local bias = 0
for i = 1, n do
  local src = t2[i]
  for j = 1, #edge[src] do
    local ej = edge[src][j]
    local dst = a[ej] + b[ej] - src
    if parent[src] == dst then
      w[dst] = w[dst] + w[src]
      if src < dst then
        bias = bias + 1
        score[src] = score[src] - 1
      else
        score[src] = score[src] + 1
      end
      ccnt[dst] = ccnt[dst] - 1
      if ccnt[dst] == 0 then
        table.insert(t2, dst)
      end
      break
    end
  end
end
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local ej = edge[src][j]
    local dst = a[ej] + b[ej] - src
    if parent[src] ~= dst then
      score[dst] = score[dst] + score[src]
    end
  end
end

for i = 1, n do
  score[i] = score[i] + bias
end
print(table.concat(score, "\n"))
0