結果

問題 No.168 ものさし
ユーザー kurenai3110kurenai3110
提出日時 2016-08-30 22:36:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,914 bytes
コンパイル時間 708 ms
コンパイル使用メモリ 83,904 KB
実行使用メモリ 13,188 KB
最終ジャッジ日時 2023-08-25 21:02:24
合計ジャッジ時間 2,165 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
5,072 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 14 ms
5,072 KB
testcase_12 AC 41 ms
11,944 KB
testcase_13 AC 57 ms
11,268 KB
testcase_14 AC 57 ms
11,956 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 4 ms
4,384 KB
testcase_19 AC 55 ms
11,976 KB
testcase_20 AC 59 ms
13,188 KB
testcase_21 AC 59 ms
11,580 KB
testcase_22 AC 58 ms
11,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;

#define MAX 2e9;

vector<pair<long long, long long> >P;
int N;

struct UnionFind
{
	vector<int> par;
	vector<int> sizes;

	UnionFind(int n) : par(n), sizes(n, 1) {
		for (int i = 0; i < n; i++) par[i] = i;
	}

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

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

		if (x == y) return;

		if (sizes[x] < sizes[y]) swap(x, y);

		par[y] = x;
		sizes[x] += sizes[y];
	}

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

	int size(int x) {
		return sizes[find(x)];
	}
};

struct Edge {
	int a, b;
	long long cost;

	bool operator<(const Edge& o) const {
		return cost < o.cost;
	}
};

struct Graph {
	vector<Edge> es;

	void esort() {
		sort(es.begin(), es.end());
	}

	bool kruskal(int d) {
		bool flag = true;

		UnionFind uf(N);

		for (int ei = 0; ei < es.size(); ei++) {
			Edge& e = es[ei];
			if (!uf.same(e.a, e.b)) {
				if (e.cost > (long long)d*d) {
					flag = false;
					break;
				}

				uf.unite(e.a, e.b);
				if (uf.same(0, N - 1)) break;
			}
		}
		return flag;
	}
};

Graph input_graph() {
	Graph g;
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			Edge e;
			e.a = i;
			e.b = j;
			e.cost = (P[i].first - P[j].first)*(P[i].first - P[j].first) + (P[i].second - P[j].second)*(P[i].second - P[j].second);
			g.es.push_back(e);
		}
	}
	return g;
}


int main()
{
	cin >> N;

	P.resize(N);
	for (int i = 0; i < N; i++) {
		int x, y;
		cin >> x >> y;
		P[i] = make_pair(x, y);
	}

	Graph g = input_graph();

	g.esort();

	long long left = 0;
	long long right = MAX;
	int mid;
	while (left < right) {
		mid = (left + right) / 2;
		if (g.kruskal(mid)) {
			right = mid;
		}
		else {
			left = mid+1;
		}
	}

	cout << (int)ceil(right/10.) * 10 << endl;

    return 0;
}

0