結果

問題 No.168 ものさし
ユーザー masamasa
提出日時 2015-06-29 23:40:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,771 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 75,260 KB
実行使用メモリ 11,052 KB
最終ジャッジ日時 2023-08-25 20:57:20
合計ジャッジ時間 2,967 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
5,368 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 34 ms
4,968 KB
testcase_12 AC 110 ms
8,512 KB
testcase_13 AC 157 ms
10,980 KB
testcase_14 AC 161 ms
10,936 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 11 ms
4,376 KB
testcase_19 AC 180 ms
10,724 KB
testcase_20 AC 186 ms
11,052 KB
testcase_21 AC 185 ms
11,036 KB
testcase_22 AC 189 ms
11,052 KB
権限があれば一括ダウンロードができます

ソースコード

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