結果

問題 No.2642 Don't cut line!
ユーザー abap34abap34
提出日時 2024-02-15 01:20:31
言語 Julia
(1.10.2)
結果
MLE  
実行時間 -
コード長 4,802 bytes
コンパイル時間 234 ms
コンパイル使用メモリ 6,940 KB
実行使用メモリ 516,008 KB
最終ジャッジ日時 2024-04-09 14:34:14
合計ジャッジ時間 69,932 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,424 ms
322,508 KB
testcase_01 AC 2,364 ms
506,976 KB
testcase_02 AC 2,551 ms
509,548 KB
testcase_03 AC 2,345 ms
510,048 KB
testcase_04 AC 2,335 ms
511,548 KB
testcase_05 MLE -
testcase_06 AC 1,658 ms
379,804 KB
testcase_07 AC 1,654 ms
381,944 KB
testcase_08 AC 1,645 ms
382,280 KB
testcase_09 AC 1,649 ms
380,340 KB
testcase_10 AC 1,639 ms
379,320 KB
testcase_11 AC 1,632 ms
379,588 KB
testcase_12 AC 1,627 ms
384,040 KB
testcase_13 AC 1,637 ms
380,108 KB
testcase_14 AC 1,625 ms
380,832 KB
testcase_15 AC 1,642 ms
381,812 KB
testcase_16 AC 2,256 ms
509,448 KB
testcase_17 AC 2,008 ms
465,340 KB
testcase_18 AC 2,220 ms
476,508 KB
testcase_19 AC 1,884 ms
434,264 KB
testcase_20 AC 1,697 ms
374,276 KB
testcase_21 AC 1,709 ms
384,168 KB
testcase_22 AC 1,939 ms
444,060 KB
testcase_23 AC 2,215 ms
500,956 KB
testcase_24 AC 1,744 ms
374,064 KB
testcase_25 AC 1,698 ms
378,720 KB
testcase_26 AC 1,664 ms
379,604 KB
testcase_27 AC 1,871 ms
424,472 KB
testcase_28 AC 2,227 ms
478,532 KB
testcase_29 AC 1,679 ms
384,348 KB
testcase_30 AC 1,709 ms
370,592 KB
testcase_31 AC 1,936 ms
446,924 KB
testcase_32 AC 1,690 ms
371,940 KB
testcase_33 AC 1,345 ms
324,768 KB
testcase_34 AC 1,343 ms
324,288 KB
testcase_35 AC 1,338 ms
322,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const INF = 10^16

struct UnionFind
    par::Vector{Int}
    size::Vector{Int}
    UnionFind(N) = new(collect(1:N), collect(1:N))
end

function root!(uf::UnionFind, x::Int)
    if uf.par[x] == x
        return x
    else
        return uf.par[x] = root!(uf, uf.par[x])
    end
end

function issame!(uf::UnionFind, x::Int, y::Int)
    return root!(uf, x) == root!(uf, y)
end

function unite!(uf::UnionFind, x::Int, y::Int)
    x = root!(uf, x)
    y = root!(uf, y)
    (x == y) && (return true)
    if (uf.size[x] < uf.size[y])
        uf.par[x] = y
        uf.size[y] += uf.size[x]
    else
        uf.par[y] = x
        uf.size[x] += uf.size[y]
    end
    return true
end


struct Edge
    from::Int
    to::Int
    weight::Int
    profit::Int
end

function reverse(edge::Edge)
    return Edge(edge.to, edge.from, edge.weight, edge.profit)
end

struct Graph
    edges::Vector{Vector{Edge}}
    function Graph(N)
        edges = [Vector{Edge}() for _ in 1:N]
        new(edges)
    end
end

function all_edges(graph::Graph)::Vector{Edge}
    return collect(Iterators.flatten(graph.edges))
end


function add!(graph::Graph, edge::Edge; has_dir=false)
    push!(graph.edges[edge.from], edge)

    if !has_dir
        rev_edge = reverse(edge)
        push!(graph.edges[edge.to], rev_edge)
    end
end


function as_rooted_tree(graph::Graph, root::Int)::Graph
    N = length(graph.edges)

    tree = Graph(N)

    function dfs(v, p)
        for u in graph.edges[v]
            if u.to != p
                add!(tree, u, has_dir=true)
                dfs(u.to, v)
            end
        end
    end

    dfs(root, -1)

    return tree
end

function kruskal(graph::Graph; by=identity, rev=false)::Tuple{Graph,Int,Int}
    N = length(graph.edges)

    edges = sort(all_edges(graph), by=by, rev=rev)

    uf = UnionFind(length(graph.edges))

    graph = Graph(N)
    cost = 0
    profit = -1

    for edge in edges
        if !issame!(uf, edge.from, edge.to)
            unite!(uf, edge.from, edge.to)
            add!(graph, edge)
            cost += edge.weight
            profit = max(profit, edge.profit)
        end
    end

    return graph, cost, profit
end


struct LCA
    parent::Vector{Vector{Int}}
    dist::Vector{Vector{Int}}
    depth::Vector{Int}
    K::Int
    V::Int
    function LCA(graph::Graph; root=1)
        V = length(graph.edges)
        K = ceil(Int, log2(V))

        tree = as_rooted_tree(graph, root)

        parent = [fill(-1, V) for _ in 1:K]
        dist = [fill(-1, V) for _ in 1:K]
        depth = fill(-1, V)

        function dfs(v, p, d)
            depth[v] = d
            parent[1][v] = p
            for u in tree.edges[v]
                if u.to != p
                    dist[1][u.to] = u.weight
                    dfs(u.to, v, d + 1)
                end
            end
        end

        dfs(root, -1, 0)

        for k in 1:K-1
            for v in 1:V
                if parent[k][v] == -1
                    continue
                else
                    parent[k+1][v] = parent[k][parent[k][v]]
                    dist[k+1][v] = max(dist[k][v], dist[k][parent[k][v]])
                end
            end
        end

        return new(parent, dist, depth, K, V)
    end
end

function max_cost(lca::LCA, u::Int, v::Int)::Int
    cost = -INF

    if lca.depth[u] < lca.depth[v]
        u, v = v, u
    end

    diff = lca.depth[u] - lca.depth[v]

    for k in 0:lca.K-1
        if (diff >> k) & 1 == 1
            cost = max(cost, lca.dist[k+1][u])
            u = lca.parent[k+1][u]
        end
    end

    if u == v
        return cost
    end


    for k in lca.K:-1:1
        if lca.parent[k][u] != lca.parent[k][v]
            cost = max(cost, lca.dist[k][u])
            cost = max(cost, lca.dist[k][v])

            u = lca.parent[k][u]
            v = lca.parent[k][v]
        end
    end

    cost = max(cost, lca.dist[1][u])
    cost = max(cost, lca.dist[1][v])
    return cost
end




function main()
    N, K, C = parse.(Int, split(readline()))
    graph = Graph(N)
    for _ in 1:K
        u, v, w, p = parse.(Int, split(readline()))
        edge = Edge(u, v, w, p)
        add!(graph, edge)
    end

    mst, C_m, P_m = kruskal(graph, by=(x -> x.weight))

    mst_edges_set = Set{Edge}(all_edges(mst))

    mst_tree = as_rooted_tree(mst, 1)

    lca = LCA(mst_tree)

    cand_edges = all_edges(graph)

    sort!(cand_edges, by=(x -> x.profit), rev=true)

    for edge in cand_edges
        P = edge.profit
        if (edge in mst_edges_set)
            C_add = 0
            C_rem = 0
        else
            u, v, C_add = edge.from, edge.to, edge.weight
            C_rem = max_cost(lca, u, v)
        end

        if C_m + C_add - C_rem <= C
            println(P)
            return
        end
    end

    println(-1)
end

main()
0