結果

問題 No.2642 Don't cut line!
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-12-15 01:30:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 888 ms / 4,000 ms
コード長 4,546 bytes
コンパイル時間 493 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 150,096 KB
最終ジャッジ日時 2024-12-15 01:30:56
合計ジャッジ時間 19,152 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,564 KB
testcase_01 AC 778 ms
148,684 KB
testcase_02 AC 774 ms
150,096 KB
testcase_03 AC 737 ms
149,804 KB
testcase_04 AC 771 ms
148,424 KB
testcase_05 AC 779 ms
149,960 KB
testcase_06 AC 313 ms
91,372 KB
testcase_07 AC 341 ms
93,420 KB
testcase_08 AC 343 ms
93,412 KB
testcase_09 AC 352 ms
93,420 KB
testcase_10 AC 329 ms
93,420 KB
testcase_11 AC 330 ms
93,416 KB
testcase_12 AC 381 ms
94,072 KB
testcase_13 AC 325 ms
93,544 KB
testcase_14 AC 349 ms
93,820 KB
testcase_15 AC 348 ms
94,056 KB
testcase_16 AC 486 ms
96,588 KB
testcase_17 AC 652 ms
138,244 KB
testcase_18 AC 744 ms
144,376 KB
testcase_19 AC 574 ms
127,148 KB
testcase_20 AC 484 ms
104,988 KB
testcase_21 AC 360 ms
92,160 KB
testcase_22 AC 383 ms
91,412 KB
testcase_23 AC 888 ms
149,124 KB
testcase_24 AC 518 ms
105,548 KB
testcase_25 AC 463 ms
103,984 KB
testcase_26 AC 425 ms
94,712 KB
testcase_27 AC 643 ms
123,496 KB
testcase_28 AC 815 ms
143,564 KB
testcase_29 AC 487 ms
96,800 KB
testcase_30 AC 453 ms
102,072 KB
testcase_31 AC 719 ms
131,780 KB
testcase_32 AC 444 ms
100,812 KB
testcase_33 AC 44 ms
54,848 KB
testcase_34 AC 45 ms
55,640 KB
testcase_35 AC 45 ms
54,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1308

from collections import deque

class UnionFind:
    """
    UnionFindの基本的な処理を実装したクラス
    """

    def __init__(self, size):
        self.root = [i for i in range(size)]
        self.size = [1] * size

    def get_root(self, v):
        if v == self.root[v]:
            return v
        else:
            old_root = self.root[v]
            new_root = self.get_root(old_root)
            self.root[v] = new_root
            return new_root

    def merge(self, u, v):
        root_u = self.get_root(u)
        root_v = self.get_root(v)
        if root_u == root_v:
            return False

        if self.size[root_u] >= self.size[root_v]:
            self.size[root_u] += self.size[root_v]
            self.root[root_v] = root_u
            self.root[v] = root_u
        else:
            self.size[root_v] += self.size[root_u]
            self.root[root_u] = root_v
            self.root[u] = root_v
        return True
    

def main():
    N, K, C = map(int, input().split())
    edges = []
    for _ in range(K):
        u, v, w, p = map(int, input().split())
        edges.append((u - 1, v - 1, w, p))
    if N == 1:
        print(0)
        return

    # 最小全域木を計算
    edges.sort(key=lambda x : x[2])
    uf = UnionFind(N)
    used = [False] * K
    answer = 0
    mst_weight = 0
    for i in range(K):
        u, v, w, p = edges[i]
        if uf.merge(u, v):
            mst_weight += w
            answer = max(answer, p)
            used[i] = True

    if mst_weight > C:
        print(-1)
        return
    
    
    # オイラーツアー計算
    next_nodes = [[] for _ in range(N)]
    for i in range(K):
        if used[i]:
            u, v, w, p = edges[i]
            next_nodes[u].append((v, w, p))
            next_nodes[v].append((u, w, p))

    parents = [-2] * N
    parents_weigths = [-2] * N
    parents[0] = -1
    parents_weigths[0] = -1
    depths = [0] * N
    stack = deque()
    stack.append((0, 0))

    while len(stack) > 0:
        v, index = stack.pop()

        while index < len(next_nodes[v]):
            next_v = next_nodes[v][index][0]
            w = next_nodes[v][index][1]
            if parents[v] == next_v:
                index += 1
                continue

            parents[next_v] = v
            parents_weigths[next_v] = w
            depths[next_v] = depths[v] + 1
            stack.append((v, index + 1))
            stack.append((next_v, 0))
            break

    max_depth = max(depths)
    k = 0
    while (1 << k) < max_depth:
        k += 1
    max_k = k + 1

    parents_list = [[-1] * N for _ in range(max_k)]
    parents_weights_list = [[-1] * N for _ in range(max_k)]
    parents_list[0] = parents
    parents_weights_list[0] = parents_weigths
    for k in range(1, max_k):
        for i in range(N):
            p = parents_list[k - 1][i]
            w = parents_weights_list[k - 1][i]
            if p != -1:
                parents_list[k][i] = parents_list[k - 1][p]
                parents_weights_list[k][i] = max(parents_weights_list[k - 1][p], w)
    
    # 価値が高い順に
    for i in range(K):
        if used[i]:
            continue

        u, v, w_base, p = edges[i]
        w0 = -1
        if p > answer:
            if depths[v] < depths[u]:
                u, v = v, u
            
            if depths[u] < depths[v]:
                d = depths[v] - depths[u]
                for k in reversed(range(max_k)):
                    if d >= (1 << k):
                        w0 = max(w0, parents_weights_list[k][v])
                        v = parents_list[k][v]
                        d -= 1 << k
            
            if u == v:
                if mst_weight - w0 + w_base <= C:
                    answer = p
                    continue

            for k in reversed(range(max_k)):
                if parents_list[k][u] == -1 or parents_list[k][v] == -1:
                    continue

                if parents_list[k][u] != parents_list[k][v]:
                    w0 = max(w0, parents_weights_list[k][u])
                    w0 = max(w0, parents_weights_list[k][v])
                    u = parents_list[k][u]
                    v = parents_list[k][v]
            
            w0 = max(parents_weigths[u], w0)
            w0 = max(parents_weigths[v], w0)
            if w0 != -1 and mst_weight - w0 + w_base <= C:
                answer = p
                continue
    
    print(answer)




















    



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