結果
問題 | No.94 圏外です。(EASY) |
ユーザー |
![]() |
提出日時 | 2019-12-06 08:37:38 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 257 ms / 5,000 ms |
コード長 | 2,999 bytes |
コンパイル時間 | 2,882 ms |
コンパイル使用メモリ | 82,180 KB |
実行使用メモリ | 44,108 KB |
最終ジャッジ日時 | 2024-12-23 04:58:04 |
合計ジャッジ時間 | 7,972 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if (n == 0) { System.out.println(1); return; } Point[] points = new Point[n]; for (int i = 0; i < n; i++) { points[i] = new Point(sc.nextInt(), sc.nextInt()); } Arrays.sort(points); UnionFindTree uft = new UnionFindTree(n); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (points[j].x - points[i].x > 10) { break; } if (points[i].distance2(points[j]) <= 100) { uft.unite(i, j); } } } int max = 0; HashMap<Integer, ArrayList<Integer>> map = uft.getMap(); for (ArrayList<Integer> list : map.values()) { for (int i = 0; i < list.size() - 1; i++) { for (int j = i + 1; j < list.size(); j++) { max = Math.max(max, points[list.get(i)].distance2(points[list.get(j)])); } } } System.out.println(Math.sqrt(max) + 2); } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int x) { if (x == parents[x]) { return x; } else { return parents[x] = find(parents[x]); } } public void unite(int x, int y) { int xx = find(x); int yy = find(y); if (xx == yy) { return; } parents[xx] = yy; } public HashMap<Integer, ArrayList<Integer>> getMap() { HashMap<Integer, ArrayList<Integer>> ret = new HashMap<>(); for (int i = 0; i < parents.length; i++) { int x = find(i); if (ret.containsKey(x)) { ret.get(x).add(i); } else { ArrayList<Integer> list = new ArrayList<>(); list.add(i); ret.put(x, list); } } return ret; } public String toString() { StringBuilder sb = new StringBuilder(); for (int x : parents) { sb.append(x).append(" "); } return sb.toString(); } } static class Point implements Comparable<Point> { int x; int y; public Point(int x, int y) { this.x = x; this.y = y; } public int compareTo(Point another) { if (x == another.x) { return y - another.y; } else { return x - another.x; } } public int distance2(Point another) { return (x - another.x) * (x - another.x) + (y - another.y) * (y - another.y); } } }