結果
問題 | No.2179 Planet Traveler |
ユーザー |
![]() |
提出日時 | 2023-06-11 02:45:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 84 ms / 3,000 ms |
コード長 | 1,971 bytes |
コンパイル時間 | 1,305 ms |
コンパイル使用メモリ | 118,288 KB |
最終ジャッジ日時 | 2025-02-14 01:27:59 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#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;}