結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,712 KB
testcase_01 AC 21 ms
8,656 KB
testcase_02 AC 22 ms
8,636 KB
testcase_03 AC 20 ms
8,740 KB
testcase_04 AC 20 ms
8,680 KB
testcase_05 AC 21 ms
8,772 KB
testcase_06 AC 50 ms
9,972 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 AC 648 ms
39,432 KB
testcase_12 AC 645 ms
39,316 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 623 ms
36,116 KB
testcase_16 TLE -
testcase_17 AC 645 ms
42,336 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