結果

問題 No.860 買い物
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-23 12:32:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 865 ms / 1,000 ms
コード長 1,172 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 87,328 KB
実行使用メモリ 105,652 KB
最終ジャッジ日時 2023-08-28 22:35:23
合計ジャッジ時間 9,898 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,052 KB
testcase_01 AC 76 ms
71,244 KB
testcase_02 AC 74 ms
71,236 KB
testcase_03 AC 73 ms
71,064 KB
testcase_04 AC 73 ms
70,924 KB
testcase_05 AC 75 ms
70,960 KB
testcase_06 AC 126 ms
78,504 KB
testcase_07 AC 778 ms
105,632 KB
testcase_08 AC 769 ms
105,288 KB
testcase_09 AC 771 ms
105,032 KB
testcase_10 AC 784 ms
105,652 KB
testcase_11 AC 270 ms
100,244 KB
testcase_12 AC 271 ms
100,428 KB
testcase_13 AC 691 ms
103,144 KB
testcase_14 AC 865 ms
105,368 KB
testcase_15 AC 310 ms
103,924 KB
testcase_16 AC 725 ms
103,896 KB
testcase_17 AC 248 ms
99,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        self.root[x] = self.find(self.root[x])
        return self.root[x]

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

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


N = int(input())
ans = 0
edge = []
for i in range(N):
    c, d = map(int, input().split())
    ans += c
    edge.append((c, i, N))
    if i:
        edge.append((d, i, i - 1))
edge.sort()

uf = UF_tree(N + 1)
for c, a, b in edge:
    if uf.unite(a, b):
        ans += c
print(ans)
0