結果

問題 No.1094 木登り / Climbing tree
ユーザー S97323424S97323424
提出日時 2020-10-19 00:55:33
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,240 ms / 2,000 ms
コード長 2,400 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 5,452 KB
実行使用メモリ 76,480 KB
最終ジャッジ日時 2023-08-08 01:32:40
合計ジャッジ時間 27,155 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1,030 ms
67,400 KB
testcase_02 AC 286 ms
64,192 KB
testcase_03 AC 203 ms
9,032 KB
testcase_04 AC 317 ms
34,500 KB
testcase_05 AC 586 ms
65,196 KB
testcase_06 AC 560 ms
25,004 KB
testcase_07 AC 1,158 ms
67,500 KB
testcase_08 AC 1,101 ms
67,444 KB
testcase_09 AC 1,135 ms
67,300 KB
testcase_10 AC 1,091 ms
67,448 KB
testcase_11 AC 1,206 ms
74,404 KB
testcase_12 AC 1,173 ms
74,880 KB
testcase_13 AC 1,239 ms
75,632 KB
testcase_14 AC 1,217 ms
74,656 KB
testcase_15 AC 616 ms
32,528 KB
testcase_16 AC 903 ms
76,480 KB
testcase_17 AC 759 ms
48,272 KB
testcase_18 AC 716 ms
45,696 KB
testcase_19 AC 835 ms
51,476 KB
testcase_20 AC 1,240 ms
75,560 KB
testcase_21 AC 763 ms
48,848 KB
testcase_22 AC 1,113 ms
67,420 KB
testcase_23 AC 1,098 ms
67,444 KB
testcase_24 AC 1,057 ms
67,352 KB
testcase_25 AC 1,047 ms
67,476 KB
testcase_26 AC 1,060 ms
67,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local Deque={}

Deque.lpush=function(self,value)
    local first=self.first-1
    self.first=first
    self[first]=value
end

Deque.rpush=function(self,value)
    local last=self.last+1
    self.last=last
    self[last]=value
end

Deque.lpop=function(self)
    local first=self.first
    local value=self[first]
    self[first]=nil
    self.first=first+1
    return value
end

Deque.rpop=function(self)
    local last=self.last
    local value=self[last]
    self[last]=nil
    self.last=last-1
    return value
end

Deque.size=function(self)
    return self.last-self.first+1
end

Deque.empty=function(self)
    return self.first>self.last
end

Deque.new=function()
    return setmetatable({first=0,last=-1},{__index=Deque})
end

----------

local INF=10^20

local function bfs(graph,start)
    local dist={}
    local rank={0}
    local par={}
    for i=1,#graph do
        dist[i]=INF
        par[i]=start
    end
    local que=Deque:new()
    dist[start]=0
    que:rpush(start)
    
    while not que:empty() do
        local from=que:lpop()
        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
                que:rpush(to)
            end
        end
    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]=bfs(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