結果

問題 No.2179 Planet Traveler
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-11 02:45:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 3,000 ms
コード長 1,971 bytes
コンパイル時間 1,308 ms
コンパイル使用メモリ 121,268 KB
実行使用メモリ 17,100 KB
最終ジャッジ日時 2023-08-31 01:41:18
合計ジャッジ時間 3,603 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 42 ms
17,100 KB
testcase_12 AC 74 ms
15,936 KB
testcase_13 AC 81 ms
16,368 KB
testcase_14 AC 40 ms
15,696 KB
testcase_15 AC 38 ms
16,076 KB
testcase_16 AC 39 ms
16,820 KB
testcase_17 AC 80 ms
16,628 KB
testcase_18 AC 71 ms
16,052 KB
testcase_19 AC 74 ms
15,560 KB
testcase_20 AC 11 ms
4,740 KB
testcase_21 AC 72 ms
16,380 KB
testcase_22 AC 46 ms
9,800 KB
testcase_23 AC 37 ms
9,200 KB
testcase_24 AC 57 ms
9,728 KB
testcase_25 AC 47 ms
9,600 KB
testcase_26 AC 65 ms
16,028 KB
testcase_27 AC 11 ms
4,728 KB
testcase_28 AC 33 ms
9,596 KB
testcase_29 AC 59 ms
10,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>

using namespace std;
using ll = long long;

struct UnionFind {
    int ngroup;
    vector<int> par;
    vector<int> siz;

    UnionFind(int N) : par(N), siz(N) {
        ngroup = N;
        for(int i = 0; i < N; i++){
            par[i] = i;
            siz[i] = 1;
        }
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    int unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return rx;
        ngroup--;
        if (siz[rx] > siz[ry]) swap(rx, ry);
        par[rx] = ry;
        siz[ry] += siz[rx];
        return ry;
    }

    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    int size(int x){
        return siz[root(x)];
    }

    int group_count(){
        return ngroup;
    }
};

ll s(ll x){
    return x*x;
}

ll isqrt(ll s){
    ll l=0, r=3e9, c;
    while(r-l>1){
        c = (l+r)/2;
        if (c*c <= s) l = c;
        else r = c;
    }
    return l;
}

int main(){
 
    ll N, C, ans=0, R1, R2;
    cin >> N;
    vector<tuple<ll, ll, ll>> v;
    vector<ll> x(N), y(N), t(N);
    for (int i=0; i<N; i++) cin >> x[i] >> y[i] >> t[i];
    UnionFind tree(N+1);
    
    for (int i=0; i<N; i++){
        for (int j=i+1; j<N; j++){
            if (t[i] != t[j]){
                R1 = s(x[i]) + s(y[i]);
                R2 = s(x[j]) + s(y[j]);
                C = R1+R2-isqrt(R1*R2*4);
            }
            else C = s(x[i]-x[j])+s(y[i]-y[j]);
            v.push_back({C, i+1, j+1});
        }
    }
    sort(v.begin(), v.end());
    for (auto [c, i, j] : v){
        if (!tree.same(1, N)){
            tree.unite(i, j);
            ans = c;
        }
    }

    cout << ans << endl;
 
    return 0;
}
0