結果

問題 No.168 ものさし
ユーザー masa
提出日時 2015-06-29 23:40:18
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 1,771 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 73,484 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-12-24 07:09:33
合計ジャッジ時間 2,982 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

typedef pair<long long, long long> PLL;

vector<PLL> points;
vector< vector<long long> > length2;

class UnionFind {
private:
	vector<int> parent;
	int trees;

public:
	UnionFind() {
	}

	UnionFind(int n) {
		trees = n;
		parent.assign(n, 0);

		for (int i = 0; i < n; i++) {
			parent[i] = i;
		}
	}

	int find(int x) {
		if (parent[x] == x) {
			return x;
		} else {
			return parent[x] = find(parent[x]);
		}
	}

	bool same(int x, int y) {
		return find(x) == find(y);
	}

	void unite(int x, int y) {
		x = find(x);
		y = find(y);

		if (x != y) {
			parent[x] = y;
			trees--;
		}
	}

	int count() {
		return trees;
	}
};

bool possible(long long len) {
	int n = points.size();
	long long len2 = len * len;
	UnionFind uf(n);

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (uf.same(i, j)) {
				continue;
			}

			if (len2 >= length2[i][j]) {
				uf.unite(i, j);
			}
		}
	}

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

int main() {
	int n, x, y;

	cin >> n;
	points.assign(n, PLL());

	for (int i = 0; i < n; i++) {
		cin >> x >> y;
		points[i] = make_pair(x, y);
	}

	length2.assign(n, vector<long long>(n, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			long long x = points[i].first  - points[j].first;
			long long y = points[i].second - points[j].second;
			length2[i][j] = x * x + y * y;
		}
	}

	// 条件を満たさない最大長をさがす
	long long length = 0;
	for (int i = 31; i >= 0; i--) {
		long long tmp = length + (1LL << i);

		if (!possible(tmp)) {
			length = tmp;
		}
	}

	long long ans = length + 1;
	if (ans % 10 != 0) {
		ans = (ans / 10 + 1) * 10;
	}

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