local function meld(node1,node2) if not node1 then return node2 elseif not node2 then return node1 elseif node1[1]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)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