結果

問題 No.2179 Planet Traveler
ユーザー ぷらぷら
提出日時 2023-01-06 22:36:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 108 ms / 3,000 ms
コード長 2,078 bytes
コンパイル時間 2,823 ms
コンパイル使用メモリ 212,036 KB
実行使用メモリ 17,184 KB
最終ジャッジ日時 2023-08-20 15:53:44
合計ジャッジ時間 5,207 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 58 ms
17,004 KB
testcase_12 AC 106 ms
15,736 KB
testcase_13 AC 108 ms
15,924 KB
testcase_14 AC 53 ms
15,528 KB
testcase_15 AC 55 ms
16,456 KB
testcase_16 AC 54 ms
15,540 KB
testcase_17 AC 103 ms
16,084 KB
testcase_18 AC 100 ms
16,492 KB
testcase_19 AC 100 ms
15,792 KB
testcase_20 AC 14 ms
4,720 KB
testcase_21 AC 93 ms
15,880 KB
testcase_22 AC 60 ms
10,180 KB
testcase_23 AC 56 ms
9,236 KB
testcase_24 AC 80 ms
10,708 KB
testcase_25 AC 64 ms
10,320 KB
testcase_26 AC 92 ms
17,184 KB
testcase_27 AC 16 ms
4,812 KB
testcase_28 AC 44 ms
10,584 KB
testcase_29 AC 81 ms
10,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
    vector<int> par;
    vector<int> size;
    UnionFind(int n) {
        par.resize(n);
        size.resize(n,1);
        for(int i = 0; i < n; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return false;
        }
        if(size[x] > size[y]) {
            swap(x,y);
        }
        par[x] = y;
        size[y] += size[x];
        return true;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<long long>X(N),Y(N),T(N);
    for(int i = 0; i < N; i++) {
        cin >> X[i] >> Y[i] >> T[i];
    }
    vector<array<long long,3>>tmp;
    for(int i = 0; i < N; i++) {
        for(int j = i+1; j < N; j++) {
            int a = X[i]*X[i]+Y[i]*Y[i];
            int b = X[j]*X[j]+Y[j]*Y[j];
            if(T[i] == T[j]) {
                long long c = (X[i]-X[j])*(X[i]-X[j])+(Y[i]-Y[j])*(Y[i]-Y[j]);
                tmp.push_back({c,i,j});
            }
            else {
                if(a < b) swap(a,b);
                long long l = 0,r = 2001001001;
                while(l+1 < r) {
                    long long mid = (l+r)/2;
                    if(mid*mid <= 4ll*a*b) {
                        l = mid;
                    }
                    else {
                        r = mid;
                    }
                }
                long long c = a+b-l;
                tmp.push_back({c,i,j});
            }
        }
    }
    sort(tmp.begin(),tmp.end());
    UnionFind uf(N);
    for(int i = 0; i < tmp.size(); i++) {
        uf.unite(tmp[i][1],tmp[i][2]);
        if(uf.same(0,N-1)) {
            cout << tmp[i][0] << endl;
            return 0;
        }
    }
}
0