結果

問題 No.1094 木登り / Climbing tree
ユーザー S97323424S97323424
提出日時 2020-10-18 14:38:14
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,457 ms / 2,000 ms
コード長 2,888 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 5,464 KB
実行使用メモリ 95,732 KB
最終ジャッジ日時 2023-08-08 01:30:53
合計ジャッジ時間 29,889 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1,175 ms
95,316 KB
testcase_02 AC 344 ms
92,196 KB
testcase_03 AC 216 ms
9,124 KB
testcase_04 AC 414 ms
44,848 KB
testcase_05 AC 751 ms
88,840 KB
testcase_06 AC 611 ms
27,892 KB
testcase_07 AC 1,221 ms
95,288 KB
testcase_08 AC 1,180 ms
95,240 KB
testcase_09 AC 1,209 ms
95,332 KB
testcase_10 AC 1,194 ms
95,372 KB
testcase_11 AC 1,409 ms
95,732 KB
testcase_12 AC 1,408 ms
95,552 KB
testcase_13 AC 1,457 ms
95,656 KB
testcase_14 AC 1,450 ms
95,560 KB
testcase_15 AC 632 ms
32,428 KB
testcase_16 AC 948 ms
80,964 KB
testcase_17 AC 814 ms
48,088 KB
testcase_18 AC 722 ms
45,660 KB
testcase_19 AC 868 ms
53,080 KB
testcase_20 AC 1,444 ms
95,628 KB
testcase_21 AC 796 ms
48,724 KB
testcase_22 AC 1,244 ms
95,404 KB
testcase_23 AC 1,252 ms
95,284 KB
testcase_24 AC 1,168 ms
95,404 KB
testcase_25 AC 1,159 ms
95,300 KB
testcase_26 AC 1,175 ms
95,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local function meld(node1,node2)
    if not node1 then
        return node2
    elseif not node2 then
        return node1
    elseif node1[1]<node2[1] then
        node2[4]=node1[3]
        node1[3]=node2
        return node1
    else
        node1[4]=node2[3]
        node2[3]=node1
        return node2
    end
end

local function pairing(node,stack)
    if node and node[4] then
        return pairing(node[4][4],rawset(meld(node,node[4]),4,stack))
    else
        return stack,node
    end
end

local function meld_pairs(stack,node)
    if stack then
        return meld_pairs(stack[4],meld(stack,node))
    else
        return node
    end
end

local PairingHeap={}

PairingHeap.empty=function(self)
    return not self[1]
end

PairingHeap.push=function(self,key,value)
    self[1]=meld(self[1],{key,value,false,false})
end

PairingHeap.top=function(self)
    return self[1][2]
end

PairingHeap.pop=function(self)
    self[1]=meld_pairs(pairing(self[1][3],false))
end

PairingHeap.new=function()
    return setmetatable({false},{__index=PairingHeap})
end

----------

local INF=10^20

local function Dijkstra(graph,start)
    local dist={}
    local rank={0}
    local par={}
    for i=1,#graph do
        dist[i]=INF
        par[i]=start
    end
    local pq=PairingHeap:new()
    dist[start]=0
    pq:push(0,{0,start})
    
    while not pq:empty() do
        local p=pq:top() pq:pop()
        local from=p[2]
        if dist[from]<p[1] then
            goto continue
        end
        for to,cost in pairs(graph[from]) do
            if dist[to]>dist[from]+cost then
                dist[to]=dist[from]+cost
                rank[to]=rank[from]+1
                par[to]=from
                pq:push(dist[to],{dist[to],to})
            end
        end
        ::continue::
    end
    
    return dist,rank,par
end

----------

local n=io.read("*n")
local tree={}
for i=1,n do
    tree[i]={}
end
for i=1,n-1 do
    local a,b,c=io.read("*n","*n","*n")
    tree[a][b]=c
    tree[b][a]=c
end
local root=1
local dist
local rank
local par={}
dist,rank,par[1]=Dijkstra(tree,root)

local bit=require("bit")
local k=1
while bit.lshift(1,k)<n do
    k=k+1
end
for i=2,k do
    par[i]={}
    for j=1,n do
        par[i][j]=root
    end
end
for i=1,k-1 do
    for j=1,n do
        if par[i][j]~=root then
            par[i+1][j]=par[i][par[i][j]]
        end
    end
end

local q=io.read("*n")
for i=1,q do
    local s,t=io.read("*n","*n")
    local length=dist[s]+dist[t]
    if rank[s]<rank[t] then
        s,t=t,s
    end
    for j=1,k do
        if bit.band(bit.rshift((rank[s]-rank[t]),(j-1)),1)>0 then
            s=par[j][s]
        end
    end
    if s~=t then
        for j=k,1,-1 do
            if par[j][s]~=par[j][t] then
                s=par[j][s]
                t=par[j][t]
            end
        end
        s=par[1][s]
    end
    io.write(length-2*dist[s].."\n")
end

0