結果
| 問題 |
No.168 ものさし
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-02-06 17:46:44 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 70 ms / 2,000 ms |
| コード長 | 1,544 bytes |
| コンパイル時間 | 755 ms |
| コンパイル使用メモリ | 110,848 KB |
| 実行使用メモリ | 11,708 KB |
| 最終ジャッジ日時 | 2024-06-12 06:55:34 |
| 合計ジャッジ時間 | 1,927 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 19 |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math; // math functions
alias Point!long point;
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;
auto dij = new long[][](n, n);
auto maxD = long(0);
foreach (i; n.iota)
foreach (j; n.iota) {
dij[i][j] = (pi[i] - pi[j]).hypot2;
maxD = max(maxD, dij[i][j]);
}
auto calc(long _, long a) {
auto uf = new UnionFind!size_t(n);
foreach (i; n.iota)
foreach (j; n.iota)
if (dij[i][j] <= a * a)
uf.unite(i, j);
return uf.find(0) == uf.find(n - 1);
}
auto r = iota(10L, maxD.to!real.sqrt.to!long + 20, 10L).assumeSorted!calc.upperBound(0);
writeln(r.front);
}
struct Point(T) {
T x, y;
auto opBinary(string op: "+")(Point!T rhs) { return Point!T(x + rhs.x, y + rhs.y); }
auto opBinary(string op: "-")(Point!T rhs) { return Point!T(x - rhs.x, y - rhs.y); }
auto opBinary(string op: "*")(Point!T rhs) { return x * rhs.x + y * rhs.y; }
auto opBinary(string op: "*")(T a) { return Point!T(x * a, y * a); }
T hypot2() { return x ^^ 2 + y ^^ 2; }
}
class UnionFind(T) {
T[] p; // parent
T s; // sentinel
this(T 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;
}
}