結果

問題 No.168 ものさし
ユーザー lloyzlloyz
提出日時 2023-06-06 20:29:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 584 ms / 2,000 ms
コード長 2,317 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 87,168 KB
実行使用メモリ 154,948 KB
最終ジャッジ日時 2023-08-28 10:48:25
合計ジャッジ時間 7,752 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 263 ms
94,016 KB
testcase_01 AC 90 ms
72,164 KB
testcase_02 AC 90 ms
71,908 KB
testcase_03 AC 90 ms
71,976 KB
testcase_04 AC 90 ms
72,176 KB
testcase_05 AC 90 ms
72,144 KB
testcase_06 AC 92 ms
72,124 KB
testcase_07 AC 89 ms
71,912 KB
testcase_08 AC 90 ms
71,896 KB
testcase_09 AC 117 ms
77,824 KB
testcase_10 AC 125 ms
78,096 KB
testcase_11 AC 217 ms
92,204 KB
testcase_12 AC 441 ms
128,724 KB
testcase_13 AC 551 ms
153,412 KB
testcase_14 AC 522 ms
153,284 KB
testcase_15 AC 94 ms
71,880 KB
testcase_16 AC 113 ms
77,804 KB
testcase_17 AC 124 ms
77,676 KB
testcase_18 AC 144 ms
81,948 KB
testcase_19 AC 559 ms
153,128 KB
testcase_20 AC 568 ms
154,308 KB
testcase_21 AC 584 ms
154,620 KB
testcase_22 AC 584 ms
154,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush
from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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())
n = int(input())
XY = [list(map(int, input().split())) for _ in range(n)]

UF = UnionFind(n)
H = []
for i in range(n):
    for j in range(i + 1, n):
        d = (XY[i][0] - XY[j][0])**2 + (XY[i][1] - XY[j][1])**2
        heappush(H, (d, i, j))
res = 0
while H:
    d, i, j = heappop(H)
    res = d
    UF.union(i, j)
    if UF.same(0, n - 1):
        break
dd = int(res**0.5) // 10 * 10
for i in range(max(dd - 50, 10), dd + 60, 10):
    if i**2 >= res:
        print(i)
        break
0