結果

問題 No.168 ものさし
ユーザー koyumeishi
提出日時 2015-03-20 00:47:05
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 1,784 bytes
コンパイル時間 1,035 ms
コンパイル使用メモリ 84,700 KB
実行使用メモリ 11,604 KB
最終ジャッジ日時 2024-12-24 06:59:52
合計ジャッジ時間 2,507 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

class UnionFindTree{
	typedef struct {
		int parent;
		int rank;
	}base_node;
	
	vector<base_node> node;
public:
	UnionFindTree(int n){
		node.resize(n);
		for(int i=0; i<n; i++){
			node[i].parent=i;
			node[i].rank=0;
		}
	}

	int find(int x){
		if(node[x].parent == x) return x;
		else{
			return node[x].parent = find(node[x].parent);
		}
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}

	void unite(int x, int y){
		x = find(node[x].parent);
		y = find(node[y].parent);
		if(x==y) return;
		if(node[x].rank < node[y].rank){
			node[x].parent = y;
		}else if(node[x].rank > node[y].rank){
			node[y].parent = x;
		}else{
			node[x].rank++;
			unite(x,y);
		}
	}
};

struct edge{
	int u;
	int v;
	long long cost;

	bool operator<(const edge& a) const{
		return this->cost < a.cost;
	}
};

bool ok(int n, vector<edge>& E, long long len){

	UnionFindTree uft(n);

	len *= len;

	for(auto& e: E){
		if(e.cost > len) break;
		if( !uft.same(e.u, e.v) ){
			uft.unite(e.u, e.v);
		}
	}

	return uft.same(0, n-1);
}

int main(){
	int N;
	cin >> N;
	vector<int> X(N), Y(N);
	for(int i=0; i<N; i++){
		cin >> X[i] >> Y[i];
	}

	vector<edge> E;
	for(int i=0; i<N; i++){
		for(int j=i+1; j<N; j++){
			E.push_back(edge{i, j, 1LL * abs(X[i] - X[j]) * abs(X[i] - X[j]) + 1LL * abs(Y[i] - Y[j]) * abs(Y[i] - Y[j])});
		}
	}

	sort(E.begin(), E.end());

	long long lb = 0;
	long long ub = 2000000000;
	while(ub-lb>1){
		long long med = (ub+lb)/2;
		bool valid = ok(N, E, med);
		if(valid){
			ub = med;
		}else{
			lb = med;
		}
	}

	cout << ((ub+9)/10) * 10 << endl;

	return 0;
}
0