結果

問題 No.168 ものさし
ユーザー H3PO4H3PO4
提出日時 2020-05-10 09:18:52
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,580 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 244,696 KB
最終ジャッジ日時 2023-09-21 10:37:25
合計ジャッジ時間 13,291 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 490 ms
106,580 KB
testcase_01 AC 86 ms
71,844 KB
testcase_02 AC 86 ms
71,864 KB
testcase_03 AC 86 ms
71,896 KB
testcase_04 AC 88 ms
71,492 KB
testcase_05 AC 87 ms
71,956 KB
testcase_06 AC 91 ms
71,580 KB
testcase_07 AC 86 ms
71,760 KB
testcase_08 AC 85 ms
71,788 KB
testcase_09 AC 105 ms
77,776 KB
testcase_10 AC 124 ms
79,644 KB
testcase_11 AC 208 ms
84,692 KB
testcase_12 AC 405 ms
97,412 KB
testcase_13 AC 532 ms
108,612 KB
testcase_14 AC 520 ms
108,292 KB
testcase_15 AC 85 ms
71,736 KB
testcase_16 AC 103 ms
77,948 KB
testcase_17 AC 116 ms
78,068 KB
testcase_18 AC 176 ms
84,400 KB
testcase_19 AC 1,976 ms
240,700 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
# from math import isqrt
from collections import defaultdict


def isqrt(n):
    x = n
    y = (x + 1) // 2
    while y < x:
        x = y
        y = (x + n // x) // 2
    return x


ceil = lambda a, b: (a + b - 1) // b
ceil_sq = lambda n: 1 + isqrt(n - 1)


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

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


N = int(input())
P = [tuple(map(int, input().split())) for _ in range(N)]

d = defaultdict(list)
for p1, p2 in itertools.combinations(range(N), 2):
    dist = ceil_sq((P[p1][0] - P[p2][0]) ** 2 + (P[p1][1] - P[p2][1]) ** 2)
    d[ceil(dist, 10) * 10].append((p1, p2))

lst = sorted(list(d.items()))
lst.sort()

uf = UnionFind(N)
for dist, points in lst:
    for p1, p2 in points:
        uf.unite(p1, p2)
    if uf.issame(0, N - 1):
        print(dist)
        break
0