結果

問題 No.860 買い物
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-23 12:34:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 732 ms / 1,000 ms
コード長 1,225 bytes
コンパイル時間 489 ms
コンパイル使用メモリ 87,140 KB
実行使用メモリ 91,248 KB
最終ジャッジ日時 2023-08-28 22:43:06
合計ジャッジ時間 7,884 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,504 KB
testcase_01 AC 69 ms
71,416 KB
testcase_02 AC 69 ms
71,508 KB
testcase_03 AC 71 ms
71,464 KB
testcase_04 AC 71 ms
71,176 KB
testcase_05 AC 70 ms
71,252 KB
testcase_06 AC 121 ms
77,608 KB
testcase_07 AC 627 ms
91,180 KB
testcase_08 AC 645 ms
91,140 KB
testcase_09 AC 629 ms
91,084 KB
testcase_10 AC 630 ms
91,220 KB
testcase_11 AC 257 ms
90,704 KB
testcase_12 AC 260 ms
90,544 KB
testcase_13 AC 567 ms
91,248 KB
testcase_14 AC 732 ms
90,884 KB
testcase_15 AC 301 ms
90,536 KB
testcase_16 AC 631 ms
90,440 KB
testcase_17 AC 238 ms
91,232 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())
base = 10**6
ans = 0
edge = []
for i in range(N):
    c, d = map(int, input().split())
    ans += c
    edge.append((c, i*base + N))
    if i:
        edge.append((d, i*base+i-1))
edge.sort()

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