結果

問題 No.94 圏外です。(EASY)
ユーザー htensaihtensai
提出日時 2019-12-06 08:37:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 246 ms / 5,000 ms
コード長 2,999 bytes
コンパイル時間 4,309 ms
コンパイル使用メモリ 76,984 KB
実行使用メモリ 58,880 KB
最終ジャッジ日時 2023-08-24 19:42:57
合計ジャッジ時間 8,476 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,924 KB
testcase_01 AC 121 ms
55,484 KB
testcase_02 AC 122 ms
55,752 KB
testcase_03 AC 121 ms
55,724 KB
testcase_04 AC 142 ms
55,484 KB
testcase_05 AC 175 ms
56,480 KB
testcase_06 AC 183 ms
56,252 KB
testcase_07 AC 190 ms
56,688 KB
testcase_08 AC 202 ms
58,880 KB
testcase_09 AC 211 ms
42,060 KB
testcase_10 AC 214 ms
42,428 KB
testcase_11 AC 200 ms
41,824 KB
testcase_12 AC 214 ms
42,380 KB
testcase_13 AC 208 ms
41,924 KB
testcase_14 AC 212 ms
42,232 KB
testcase_15 AC 219 ms
42,728 KB
testcase_16 AC 213 ms
42,456 KB
testcase_17 AC 215 ms
42,588 KB
testcase_18 AC 210 ms
42,244 KB
testcase_19 AC 246 ms
42,508 KB
testcase_20 AC 124 ms
39,256 KB
testcase_21 AC 112 ms
39,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
       }
   }
   
}

0