結果

問題 No.2642 Don't cut line!
ユーザー rlangevinrlangevin
提出日時 2024-02-22 12:04:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 804 ms / 4,000 ms
コード長 4,051 bytes
コンパイル時間 459 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 144,252 KB
最終ジャッジ日時 2024-02-22 12:04:40
合計ジャッジ時間 22,957 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
53,460 KB
testcase_01 AC 798 ms
144,252 KB
testcase_02 AC 742 ms
143,616 KB
testcase_03 AC 787 ms
143,868 KB
testcase_04 AC 770 ms
143,744 KB
testcase_05 AC 749 ms
143,104 KB
testcase_06 AC 598 ms
95,336 KB
testcase_07 AC 530 ms
95,444 KB
testcase_08 AC 550 ms
95,424 KB
testcase_09 AC 516 ms
95,468 KB
testcase_10 AC 537 ms
95,808 KB
testcase_11 AC 515 ms
95,448 KB
testcase_12 AC 507 ms
95,912 KB
testcase_13 AC 518 ms
95,412 KB
testcase_14 AC 508 ms
95,560 KB
testcase_15 AC 571 ms
96,084 KB
testcase_16 AC 640 ms
142,884 KB
testcase_17 AC 744 ms
134,508 KB
testcase_18 AC 804 ms
140,748 KB
testcase_19 AC 577 ms
123,408 KB
testcase_20 AC 647 ms
104,808 KB
testcase_21 AC 385 ms
97,716 KB
testcase_22 AC 498 ms
125,900 KB
testcase_23 AC 780 ms
142,572 KB
testcase_24 AC 544 ms
110,764 KB
testcase_25 AC 594 ms
108,368 KB
testcase_26 AC 613 ms
100,128 KB
testcase_27 AC 551 ms
118,888 KB
testcase_28 AC 672 ms
136,452 KB
testcase_29 AC 695 ms
102,820 KB
testcase_30 AC 627 ms
107,116 KB
testcase_31 AC 597 ms
128,172 KB
testcase_32 AC 585 ms
105,284 KB
testcase_33 AC 31 ms
53,460 KB
testcase_34 AC 30 ms
53,460 KB
testcase_35 AC 30 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


class LowestCommonAncestor:
    def __init__(self, n):
        self._n = n
        self._logn = 0
        v = 1
        while v <= self._n:
            v *= 2
            self._logn += 1
        self._depth = [0] * self._n
        self._distance = [0] * self._n
        self._ancestor = [[-1] * self._n for _ in range(self._logn)]
        self.dp = [[-1] * self._n for _ in range(self._logn)]
        self._G = [[] for i in range(self._n)]
        

    def add_edge(self, u, v, w=1):
        self._G[u].append((v, w))
        self._G[v].append((u, w))

    def build(self, root=0):
        stack = [root]
        while stack:
            cur = stack.pop()
            for nex, w in self._G[cur]:
                if self._ancestor[0][nex] != cur and self._ancestor[0][cur] != nex:
                    self._ancestor[0][nex] = cur
                    self._depth[nex] = self._depth[cur] + 1
                    self._distance[nex] = self._distance[cur] + w
                    stack.append(nex)
                    self.dp[0][nex] = w 

        for k in range(1, self._logn):
            for i in range(self._n):
                if self._ancestor[k - 1][i] == -1:
                    self._ancestor[k][i] = -1
                    self.dp[k][i] = self.dp[k-1][i]
                else:
                    self._ancestor[k][i] = self._ancestor[k - 1][self._ancestor[k - 1][i]]
                    self.dp[k][i] = max(self.dp[k-1][i], self.dp[k-1][self._ancestor[k - 1][i]])

    def lca(self, u, v):
        if self._depth[u] > self._depth[v]:
            u, v = v, u
        for k in range(self._logn - 1, -1, -1):
            if ((self._depth[v] - self._depth[u]) >> k) & 1:
                v = self._ancestor[k][v]
        if u == v:
            return u
        for k in range(self._logn - 1, -1, -1):
            if self._ancestor[k][u] != self._ancestor[k][v]:
                u = self._ancestor[k][u]
                v = self._ancestor[k][v]
        return self._ancestor[0][u]

    def distance(self, u, v):
        return self._distance[u] + self._distance[v] - 2 * self._distance[self.lca(u, v)]

    def query(self, a, b):
        lca = self.lca(a, b)
        return max(self.max_edge(a, self._depth[a] - self._depth[lca])
                   , self.max_edge(b, self._depth[b] - self._depth[lca]))
        
    def max_edge(self, a, d):
        now = -10 ** 18
        for i in range(self._logn):
            if (d >> i) & 1:
                now = max(now, self.dp[i][a])
                a = self._ancestor[i][a]
        return now
        

N, M, cost = map(int, input().split())
U = UnionFind(N)
used = [0] * M
Edge = []
for i in range(M):
    A, B, C, p = map(int, input().split())
    A, B = A - 1, B - 1
    Edge.append((C, A, B, p, i))
    
Edge.sort()
base = 0
T = LowestCommonAncestor(N)
for c, a, b, p, i in Edge:
    if U.is_same(a, b):
        continue
    used[i] = 1
    U.union(a, b)
    T.add_edge(a, b, c)
    base += c
    
if base > cost:
    print(-1)
    exit()
    
ans = -1
T.build(0)
for c, a, b, p, i in Edge:
    if used[i]:
        ans = max(ans, p)
        continue
    
    if base + c - T.query(a, b) <= cost:
        ans = max(ans, p)
        
print(ans)
0