結果

問題 No.2642 Don't cut line!
ユーザー 👑 rin204rin204
提出日時 2024-02-19 22:16:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,480 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 82,792 KB
実行使用メモリ 134,268 KB
最終ジャッジ日時 2024-09-29 02:12:31
合計ジャッジ時間 15,004 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
54,168 KB
testcase_01 AC 501 ms
133,724 KB
testcase_02 AC 511 ms
133,608 KB
testcase_03 AC 535 ms
133,908 KB
testcase_04 AC 525 ms
133,736 KB
testcase_05 AC 536 ms
133,732 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 563 ms
134,268 KB
testcase_17 AC 468 ms
126,232 KB
testcase_18 AC 505 ms
130,700 KB
testcase_19 AC 392 ms
116,756 KB
testcase_20 AC 375 ms
98,172 KB
testcase_21 AC 317 ms
96,952 KB
testcase_22 AC 435 ms
119,084 KB
testcase_23 AC 541 ms
132,588 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 393 ms
112,656 KB
testcase_28 AC 517 ms
128,496 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 464 ms
121,336 KB
testcase_32 WA -
testcase_33 AC 37 ms
53,632 KB
testcase_34 AC 37 ms
53,440 KB
testcase_35 AC 37 ms
54,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.par = [-1] * n
        self.group_ = n

    def find(self, x):
        if self.par[x] < 0:
            return x
        lst = []
        while self.par[x] >= 0:
            lst.append(x)
            x = self.par[x]
        for y in lst:
            self.par[y] = x
        return x

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False

        if self.par[x] > self.par[y]:
            x, y = y, x

        self.par[x] += self.par[y]
        self.par[y] = x
        self.group_ -= 1
        return True

    def size(self, x):
        return -self.par[self.find(x)]

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

    @property
    def group(self):
        return self.group_


class WeightedLCA:
    def __init__(self, n, edges, e, ope, root=0):
        self.n = n
        self.e = e
        self.ope = ope
        self.logn = (n - 1).bit_length()
        self.root = root
        self.depth = [-1] * self.n
        self.parent = [[-1] * n for _ in range(self.logn)]
        self.weight = [[-1] * n for _ in range(self.logn)]
        self.dfs(edges)
        self.doubling()

    def dfs(self, edges):
        stack = [self.root]
        self.depth[self.root] = 0
        while stack:
            pos = stack.pop()
            for npos, w in edges[pos]:
                if self.depth[npos] != -1:
                    continue
                self.depth[npos] = self.depth[pos] + 1
                self.parent[0][npos] = pos
                self.weight[0][npos] = w
                stack.append(npos)

    def doubling(self):
        for i in range(1, self.logn):
            for j in range(self.n):
                if self.parent[i - 1][j] != -1:
                    p = self.parent[i - 1][j]
                    self.parent[i][j] = self.parent[i - 1][p]
                    self.weight[i][j] = self.ope(self.weight[i - 1][j], self.weight[i - 1][p])

    def get(self, u, v):
        if self.depth[v] < self.depth[u]:
            u, v = v, u
        du = self.depth[u]
        dv = self.depth[v]
        ret = self.e

        for i in range(self.logn):
            if (dv - du) >> i & 1:
                ret = self.ope(ret, self.weight[i][v])
                v = self.parent[i][v]

        if u == v:
            return ret, u

        for i in range(self.logn - 1, -1, -1):
            pu = self.parent[i][u]
            pv = self.parent[i][v]
            if pu != pv:
                ret = self.ope(ret, self.weight[i][u])
                ret = self.ope(ret, self.weight[i][v])
                u, v = pu, pv

        ret = self.ope(ret, self.weight[0][u])
        ret = self.ope(ret, self.weight[0][v])
        u = self.parent[0][u]
        return ret, u


n, k, C = map(int, input().split())
E = [list(map(int, input().split())) for _ in range(k)]
W = [e[2] * n + i for i, e in enumerate(E)]
W.sort()
nE = []
for w in W:
    i = w % n
    nE.append(E[i])
E = nE
edges = [[] for _ in range(n)]
UF = UnionFind(n)
tot = 0
for u, v, w, _ in E:
    u -= 1
    v -= 1
    if UF.unite(u, v):
        edges[u].append((v, w))
        edges[v].append((u, w))
        tot += w
        if UF.group == 1:
            break

lca = WeightedLCA(n, edges, 0, lambda x, y: max(x, y))
ans = -1
for u, v, w, p in E:
    u -= 1
    v -= 1
    nc = tot + w - lca.get(u, v)[0]
    if nc <= C:
        ans = max(ans, p)

print(ans)
0