各位大佬有没有julia练习题?谢谢各位大佬
Leetcode?
试了一下最小生成树的Kruskal算法若干测试点会TLE:
function main()
n, m = parse.(Int, readline() |> split)
edges = Tuple{Int, Int, Int}[]
for _ in 1:m
push!(edges, tuple(parse.(Int, readline() |> split)...))
end
sort!(edges; by = x -> x[3])
father = collect(1:n)
find_root(u::Int)::Int = (father[u] == u) ? u : (father[u] = find_root(father[u]))
issame_root(u::Int, v::Int)::Bool = find_root(u) == find_root(v)
function merge!(u::Int, v::Int)
u_root = find_root(u)
v_root = find_root(v)
if u_root != v_root
n -= 1
father[u_root] = v_root
end
end
res = 0
for (u, v, w) in edges
issame_root(u, v) && continue
res += w
merge!(u, v)
end
res
end
main() |> print
另外用DataStructures.IntDisjointSets所有点都会TLE。
收到,正在处理。
hackerrank