結果

問題 No.168 ものさし
ユーザー rrrrm99rrrrm99
提出日時 2016-11-28 22:48:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,918 bytes
コンパイル時間 695 ms
コンパイル使用メモリ 76,580 KB
実行使用メモリ 11,428 KB
最終ジャッジ日時 2023-08-25 21:04:17
合計ジャッジ時間 2,374 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
7,456 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 5 ms
4,436 KB
testcase_11 AC 26 ms
7,368 KB
testcase_12 AC 81 ms
9,956 KB
testcase_13 AC 114 ms
11,352 KB
testcase_14 AC 114 ms
11,428 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 5 ms
4,664 KB
testcase_19 AC 56 ms
11,080 KB
testcase_20 AC 58 ms
11,252 KB
testcase_21 AC 58 ms
11,352 KB
testcase_22 AC 58 ms
11,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>

class DisjointSet {
public:
    std::vector<int> rank, p;

    DisjointSet() {}
    DisjointSet(int size) {
        rank.resize(size, 0);
        p.resize(size, 0);
        for(int i = 0; i < size; i++) {
           p[i] = i;
           rank[i] = 0;
        }
    }

    bool same(int x, int y) {
        return findSet(x) == findSet(y);
    }

    void unite(int x, int y) {
        link(findSet(x), findSet(y));
    }

    void link(int x, int y) {
        if(rank[x] > rank[y]) {
            p[y] = x;
        } else {
            p[x] = y;
            if(rank[x] == rank[y]) {
                rank[y]++;
            }
        }
    }

    int findSet(int x) {
        if(x != p[x]) {
            p[x] = findSet(p[x]);
        }
        return p[x];
    }
};

int n;
long long x[1001];
long long y[1001];
long long d[1001][1001];

bool ok(long long dist) {
    DisjointSet ds(n);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(d[i][j] <= dist * dist) {
                ds.unite(i, j);
            }
        }
    }
    return ds.same(0, n-1);
}

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);

    std::cin >> n;
    for(int i = 0; i < n; i++) {
        std::cin >> x[i] >> y[i];
    }
    
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            long long dx = x[i] - x[j];
            long long dy = y[i] - y[j];
            d[i][j] = dx * dx + dy * dy;
        }
    }

    long long left = 0;
    long long right = 10000000000;
    while(left < right) {
        long long mid = (left + right) / 2;
        if(ok(mid)) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    long long ans;
    if(left % 10 != 0) {
        ans = left + (10 - (left % 10));
    } else {
        ans = left;
    }
    std::cout << ans << std::endl;
}
0