結果

問題 No.94 圏外です。(EASY)
ユーザー rpy3cpprpy3cpp
提出日時 2015-07-19 19:12:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 224 ms / 5,000 ms
コード長 1,804 bytes
コンパイル時間 79 ms
コンパイル使用メモリ 10,848 KB
実行使用メモリ 9,092 KB
最終ジャッジ日時 2023-09-08 14:32:13
合計ジャッジ時間 2,982 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,752 KB
testcase_01 AC 20 ms
8,868 KB
testcase_02 AC 20 ms
8,824 KB
testcase_03 AC 19 ms
8,956 KB
testcase_04 AC 21 ms
8,760 KB
testcase_05 AC 25 ms
8,784 KB
testcase_06 AC 32 ms
8,812 KB
testcase_07 AC 47 ms
9,032 KB
testcase_08 AC 76 ms
8,868 KB
testcase_09 AC 127 ms
8,972 KB
testcase_10 AC 129 ms
9,056 KB
testcase_11 AC 124 ms
9,016 KB
testcase_12 AC 128 ms
8,972 KB
testcase_13 AC 127 ms
9,092 KB
testcase_14 AC 132 ms
8,912 KB
testcase_15 AC 130 ms
8,912 KB
testcase_16 AC 132 ms
8,984 KB
testcase_17 AC 128 ms
8,976 KB
testcase_18 AC 129 ms
8,916 KB
testcase_19 AC 224 ms
8,900 KB
testcase_20 AC 20 ms
8,768 KB
testcase_21 AC 19 ms
8,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
import itertools
import collections

class DisjointSet(object):
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n
        self.num = n  # number of disjoint sets

    def union(self, x, y):
        self._link(self.find_set(x), self.find_set(y))

    def _link(self, x, y):
        if x == y:
            return
        self.num -= 1
        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
        else:
            self.parent[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def find_set(self, x):
        xp = self.parent[x]
        if xp != x:
            self.parent[x] = self.find_set(xp)
        return self.parent[x]


def read_data():
    N = int(input())
    xy = []
    for n in range(N):
        x, y = map(int, input().split())
        xy.append((x, y))
    return N, xy


def solve(N, xy):
    if N == 0:
        return 1
    if N == 1:
        return 2
    djs = DisjointSet(N)
    for i in range(N-1):
        xi, yi = xy[i]
        for j in range(i+1, N):
            xj, yj = xy[j]
            if (xi-xj)**2 + (yi-yj)**2 <= 100:
                djs.union(i, j)
    n_groups = djs.num
    if n_groups == N:
        return 2
    groups = collections.defaultdict(list)
    for i in range(N):
        groups[djs.find_set(i)].append(i)
    max_dist = -1
    for group in groups.values():
        if len(group) == 1:
            dist = 1
            if dist > max_dist:
                max_dist = dist
        for i, j in itertools.combinations(group, 2):
            xi, yi = xy[i]
            xj, yj = xy[j]
            dist = math.hypot(xi-xj, yi-yj)
            if dist > max_dist:
                max_dist = dist
    return max_dist + 2

N, xy = read_data()
print(solve(N, xy))
0