結果

問題 No.860 買い物
ユーザー takeya_okinotakeya_okino
提出日時 2021-09-26 13:23:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,589 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 10,912 KB
実行使用メモリ 42,468 KB
最終ジャッジ日時 2023-09-19 02:53:02
合計ジャッジ時間 12,541 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,708 KB
testcase_01 AC 21 ms
8,640 KB
testcase_02 AC 20 ms
8,740 KB
testcase_03 AC 19 ms
8,600 KB
testcase_04 AC 20 ms
8,644 KB
testcase_05 AC 20 ms
8,720 KB
testcase_06 AC 48 ms
10,068 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 AC 636 ms
39,388 KB
testcase_12 AC 641 ms
39,328 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 625 ms
36,220 KB
testcase_16 TLE -
testcase_17 AC 653 ms
42,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())

A = []

ans = 0

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

A.sort()
 
from collections import defaultdict
 
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
 
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
 
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
 
        if x == y:
            return
 
        if self.parents[x] > self.parents[y]:
            x, y = y, x
 
        self.parents[x] += self.parents[y]
        self.parents[y] = x
 
    def size(self, x):
        return -self.parents[self.find(x)]
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
 
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
 
    def group_count(self):
        return len(self.roots())
 
    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members
 
    def __str__(self):
   	     return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

uf = UnionFind(n + 1)

for [c, a, b] in A:
  if not uf.same(a,b):
    ans += c
    uf.union(a,b)

print(ans)
0