結果

問題 No.94 圏外です。(EASY)
ユーザー te-shte-sh
提出日時 2017-05-09 11:07:08
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 1,545 bytes
コンパイル時間 2,130 ms
コンパイル使用メモリ 166,808 KB
実行使用メモリ 7,188 KB
最終ジャッジ日時 2023-09-03 13:12:45
合計ジャッジ時間 3,411 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 5 ms
5,616 KB
testcase_08 AC 9 ms
6,340 KB
testcase_09 AC 15 ms
7,084 KB
testcase_10 AC 14 ms
7,092 KB
testcase_11 AC 17 ms
7,188 KB
testcase_12 AC 14 ms
7,144 KB
testcase_13 AC 18 ms
7,132 KB
testcase_14 AC 18 ms
7,132 KB
testcase_15 AC 19 ms
7,128 KB
testcase_16 AC 18 ms
7,172 KB
testcase_17 AC 17 ms
7,128 KB
testcase_18 AC 16 ms
7,128 KB
testcase_19 AC 21 ms
7,128 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions

void main()
{
  auto n = readln.chomp.to!size_t;
  auto pi = n.iota.map!(_ => readln.split.to!(int[])).map!(rd => point(rd[0], rd[1])).array;

  if (n == 0) {
    writeln(1);
    return;
  }

  auto dij = new int[][](n, n);
  foreach (i; 0..n)
    foreach (j; i+1..n)
      dij[i][j] = dij[j][i] = (pi[i] - pi[j]).hypot2;

  auto uf = UnionFind!size_t(n);
  foreach (i; 0..n)
    foreach (j; i+1..n)
      if (i != j && dij[i][j] <= 100)
        uf.unite(i, j);

  auto maxD = uf.groups
      .map!(g => dij.indexed(g).map!(di => di.indexed(g)).joiner.fold!max)
      .fold!max;
  writefln("%.7f", maxD.to!real.sqrt + 2);
}

alias Point!int point;

struct UnionFind(T)
{
  import std.algorithm, std.range;

  T[] p; // parent
  const T s; // sentinel
  const T n;

  this(T n)
  {
    this.n = n;
    p = new T[](n);
    s = n + 1;
    p[] = s;
  }

  T find(T i)
  {
    if (p[i] == s) {
      return i;
    } else {
      p[i] = find(p[i]);
      return p[i];
    }
  }

  void unite(T i, T j)
  {
    auto pi = find(i), pj = find(j);
    if (pi != pj) p[pj] = pi;
  }

  bool isSame(T i, T j) { return find(i) == find(j); }

  auto groups()
  {
    auto g = new T[][](n);
    foreach (i; 0..n) g[find(i)] ~= i;
    return g.filter!(l => !l.empty);
  }
}

struct Point(T) {
  T x, y;

  point opBinary(string op)(point rhs) {
    static if (op == "-") return point(x - rhs.x, y - rhs.y);
  }

  T hypot2() { return x ^^ 2 + y ^^ 2; }
}
0