結果

問題 No.168 ものさし
ユーザー oxyshoweroxyshower
提出日時 2019-04-10 14:44:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,782 bytes
コンパイル時間 1,687 ms
コンパイル使用メモリ 176,880 KB
実行使用メモリ 17,056 KB
最終ジャッジ日時 2023-09-21 11:38:47
合計ジャッジ時間 3,364 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 81 ms
16,124 KB
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct edge{ int u,v,cost; };
bool comp(const edge& e1,const edge& e2){
  return e1.cost < e2.cost;
}

vector<edge> es;
int V,E; //頂点数、辺数

struct UF{
  vector<int> par; //親
  vector<int> size; //集合の大きさ

  void init(int n){
    par.resize(n);
    size.resize(n);
    for(int i = 0; i < n; i++){
      par[i] = i;
      size[i] = 1;
    }
  }

  //木の根を求める
  int root(int x){
    if(par[x] == x){
      return x;
    }
    else {
      return par[x] = root(par[x]);
    }
  }

  //xとyの属する集合を併合
  void unite(int x,int y){
    x = root(x);
    y = root(y);
    if(x == y) return;

    if(size[x] < size[y]) swap(x,y);
    par[y] = x;
    size[x] += size[y];
  }

}uf;

int kruskal(int ok){
  //sort(es.begin(),es.end(),comp);
  uf.init(V);
  int total = 0;
  for(int i = 0; i < es.size(); i++){
    edge e = es[i];
    if(e.cost > ok) break;
    if(uf.root(e.u) != uf.root(e.v)){
      total += e.cost;
      uf.unite(e.u,e.v);
    }
  }
  return uf.root(0) == uf.root(V-1);
}

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

  cin >> V;
  vector<int> x(V),y(V);
  for(int i = 0; i < V; i++){
    cin >> x[i] >> y[i];
  }

  for(int i = 0; i < V; i++){
    for(int j = i+1; j < V; j++){
      int cost = abs(x[i] - x[j])*abs(x[i] - x[j]) + abs(y[i] - y[j])*abs(y[i] - y[j]);
      es.push_back({i,j,cost});
    }
  }
  sort(es.begin(),es.end(),comp);

  function< int(int) > check =
  [&](int mid){
    if(kruskal(mid*mid)) return 1LL;
    return 0LL;
  };

  int left = -1,right = 2e9;
  while(right - left > 1){
    int mid = (left + right) / 2;

    if(check(mid)) right = mid;
    else left = mid;
  }
  cout << right << endl;

  return 0;
}
0