結果

問題 No.2642 Don't cut line!
ユーザー abap34abap34
提出日時 2024-02-17 19:44:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 3,486 ms / 4,000 ms
コード長 4,467 bytes
コンパイル時間 1,562 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 128,452 KB
最終ジャッジ日時 2024-02-20 12:46:46
合計ジャッジ時間 60,158 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,624 KB
testcase_01 AC 3,301 ms
128,336 KB
testcase_02 AC 3,360 ms
128,340 KB
testcase_03 AC 3,293 ms
128,340 KB
testcase_04 AC 3,281 ms
128,344 KB
testcase_05 AC 3,343 ms
128,340 KB
testcase_06 AC 874 ms
41,600 KB
testcase_07 AC 908 ms
41,752 KB
testcase_08 AC 868 ms
41,240 KB
testcase_09 AC 883 ms
41,880 KB
testcase_10 AC 893 ms
41,952 KB
testcase_11 AC 873 ms
41,332 KB
testcase_12 AC 907 ms
42,468 KB
testcase_13 AC 878 ms
41,140 KB
testcase_14 AC 881 ms
41,968 KB
testcase_15 AC 916 ms
42,556 KB
testcase_16 AC 3,486 ms
128,452 KB
testcase_17 AC 2,587 ms
107,956 KB
testcase_18 AC 2,779 ms
115,632 KB
testcase_19 AC 1,939 ms
86,368 KB
testcase_20 AC 1,086 ms
55,348 KB
testcase_21 AC 1,782 ms
51,636 KB
testcase_22 AC 2,355 ms
95,824 KB
testcase_23 AC 3,291 ms
126,852 KB
testcase_24 AC 1,143 ms
65,884 KB
testcase_25 AC 1,109 ms
63,028 KB
testcase_26 AC 999 ms
51,852 KB
testcase_27 AC 1,834 ms
84,172 KB
testcase_28 AC 2,967 ms
118,536 KB
testcase_29 AC 990 ms
54,820 KB
testcase_30 AC 1,094 ms
59,736 KB
testcase_31 AC 2,369 ms
99,184 KB
testcase_32 AC 1,090 ms
57,668 KB
testcase_33 AC 30 ms
10,624 KB
testcase_34 AC 29 ms
10,624 KB
testcase_35 AC 30 ms
10,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
from collections import namedtuple

INF = 10**16

class UnionFind:
    def __init__(self, N):
        self.par = list(range(N))
        self.size = [1] * N

    def root(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def issame(self, x, y):
        return self.root(x) == self.root(y)

    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return True
        if self.size[x] < self.size[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
        return True

Edge = namedtuple('Edge', ['from_', 'to', 'weight', 'profit'])

def reverse(edge):
    return Edge(edge.to, edge.from_, edge.weight, edge.profit)

class Graph:
    def __init__(self, N):
        self.edges = [[] for _ in range(N)]

    def all_edges(self):
        return [edge for edge_list in self.edges for edge in edge_list]

    def add(self, edge, has_dir=False):
        self.edges[edge.from_].append(edge)
        if not has_dir:
            self.edges[edge.to].append(reverse(edge))

def as_rooted_tree(graph, root):
    N = len(graph.edges)
    tree = Graph(N)
    stack = [(root, -1)]
    while stack:
        v, p = stack.pop()
        for u in graph.edges[v]:
            if u.to != p:
                tree.add(u, has_dir=True)
                stack.append((u.to, v))
    return tree

def kruskal(sorted_edges, N):
    uf = UnionFind(N)
    graph = Graph(N)
    cost = 0
    profit = -1
    for edge in sorted_edges:
        if not uf.issame(edge.from_, edge.to):
            uf.unite(edge.from_, edge.to)
            graph.add(edge)
            cost += edge.weight
            profit = max(profit, edge.profit)
    return graph, cost, profit

class LCA:
    def __init__(self, graph, root=0):
        V = len(graph.edges)
        K = math.ceil(math.log2(V))
        tree = as_rooted_tree(graph, root)
        self.parent = [[-1] * V for _ in range(K)]
        self.dist = [[-1] * V for _ in range(K)]
        self.depth = [-1] * V
        self.K = K
        self.V = V
        stack = [(root, -1, 0)]
        while stack:
            v, p, d = stack.pop()
            self.depth[v] = d
            self.parent[0][v] = p
            for u in tree.edges[v]:
                if u.to != p:
                    self.dist[0][u.to] = u.weight
                    stack.append((u.to, v, d + 1))
        for k in range(K - 1):
            for v in range(V):
                if self.parent[k][v] != -1:
                    self.parent[k + 1][v] = self.parent[k][self.parent[k][v]]
                    self.dist[k + 1][v] = max(self.dist[k][v], self.dist[k][self.parent[k][v]])

    def max_cost(self, u, v):
        cost = -INF
        if self.depth[u] < self.depth[v]:
            u, v = v, u
        diff = self.depth[u] - self.depth[v]
        for k in range(self.K):
            if (diff >> k) & 1:
                cost = max(cost, self.dist[k][u])
                u = self.parent[k][u]
        if u == v:
            return cost
        for k in range(self.K - 1, -1, -1):
            if self.parent[k][u] != self.parent[k][v]:
                cost = max(cost, self.dist[k][u])
                cost = max(cost, self.dist[k][v])
                u = self.parent[k][u]
                v = self.parent[k][v]
        cost = max(cost, self.dist[0][u])
        cost = max(cost, self.dist[0][v])
        return cost

def main():
    N, K, C = map(int, input().split())
    graph = Graph(N)
    for _ in range(K):
        u, v, w, p = map(int, input().split())
        u -= 1
        v -= 1
        edge = Edge(u, v, w, p)
        graph.add(edge)
    sorted_edges = graph.all_edges()
    sorted_edges.sort(key=lambda x: x.weight)
    mst, C_m, P_m = kruskal(sorted_edges, N)
    mst_edges_set = set(mst.all_edges())
    mst_tree = as_rooted_tree(mst, 0)
    lca = LCA(mst_tree)
    sorted_edges.sort(key=lambda x: x.profit, reverse=True)
    for edge in sorted_edges:
        P = edge.profit
        if edge in mst_edges_set:
            C_add = 0
            C_rem = 0
        else:
            u, v = edge.from_, edge.to
            C_add = edge.weight
            C_rem = lca.max_cost(u, v)
        if C_m + C_add - C_rem <= C:
            print(P)
            return
    print(-1)

if __name__ == "__main__":
    main()
0