結果

問題 No.2642 Don't cut line!
ユーザー 👑 rin204rin204
提出日時 2024-02-19 22:02:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,476 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 133,564 KB
最終ジャッジ日時 2024-02-19 22:02:54
合計ジャッジ時間 17,614 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 616 ms
133,308 KB
testcase_01 AC 616 ms
133,308 KB
testcase_02 AC 644 ms
133,564 KB
testcase_03 AC 618 ms
133,180 KB
testcase_04 AC 642 ms
133,324 KB
testcase_05 WA -
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 AC 689 ms
133,480 KB
testcase_16 AC 541 ms
125,572 KB
testcase_17 AC 617 ms
130,304 KB
testcase_18 AC 453 ms
116,476 KB
testcase_19 AC 477 ms
97,332 KB
testcase_20 AC 367 ms
96,672 KB
testcase_21 AC 506 ms
118,696 KB
testcase_22 AC 675 ms
132,712 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 511 ms
112,260 KB
testcase_27 AC 637 ms
128,116 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 545 ms
120,560 KB
testcase_31 WA -
testcase_32 AC 39 ms
53,460 KB
testcase_33 AC 38 ms
53,460 KB
testcase_34 AC 47 ms
53,460 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])

edges = [[] for _ in range(n)]
UF = UnionFind(n)
tot = 0
for u, v, w, _ in nE:
    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 nE:
    u -= 1
    v -= 1
    nc = tot + w - lca.get(u, v)[0]
    if nc <= C:
        ans = max(ans, p)

print(ans)
0