Submission #2717223


Source Code Expand

#include <bits/stdc++.h>
#define INF 1000000000
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PP = pair<P, int>;

class UnionFindTree{
	private:
		int N;
		unique_ptr<int[]> parent;
		unique_ptr<int[]> rank;
		unique_ptr<int[]> size;
	public:
		UnionFindTree(int N){
			this -> N = N;
			this -> parent = make_unique<int[]>(N);
			this -> rank = make_unique<int[]>(N);
			this -> size = make_unique<int[]>(N);
			for(int i = 0; i < (int) N; i++){
				parent[i] = i; rank[i] = 0; size[i] = 1;
			}
		}
		void init(){
			for(int i = 0; i < (int) N; i++){
				parent[i] = i; rank[i] = 0; size[i] = 1;
			}
		}
		int find(int x){
			if(parent[x] == x) return x;
			else return parent[x] = find(parent[x]);
		}
		void unite(int x, int y){
			x = find(x); y = find(y);
			if(x == y) return;

			if(rank[x] < rank[y]){
				parent[x] = y;
				size[y] += size[x];
			}else{
				parent[y] = x;
				size[x] += size[y];
				if(rank[x] == rank[y]) rank[x]++;
			}
		}
		bool same(int x, int y){
			return find(x) == find(y);
		}
		int count(int x){
			return size[find(x)];
		}
};

struct edge{
	int from, to, cost;
};

bool comp(PP p1, PP p2){
	return p1.first.second < p2.first.second;
}

bool compEdge(edge e1, edge e2){
	return e1.cost < e2.cost;
}

int main(void){
	int N; cin >> N;
	vector<PP> points(N);
	for(int i = 0; i < N; i++){
		cin >> points[i].first.first >> points[i].first.second;
		points[i].second = i;
	}

	sort(begin(points), end(points));
	vector<edge> G;
	for(int i = 0; i < N-1; i++){
		PP p1 = points[i], p2 = points[i+1];
		int c = min(abs(p1.first.first - p2.first.first), abs(p1.first.second - p2.first.second));
		edge e = {p1.second, p2.second, c};
		G.push_back(e);
	}

	sort(begin(points), end(points), comp);
	for(int i = 0; i < N-1; i++){
		PP p1 = points[i], p2 = points[i+1];
		int c = min(abs(p1.first.first - p2.first.first), abs(p1.first.second - p2.first.second));
		edge e = {p1.second, p2.second, c};
		G.push_back(e);
	}

	UnionFindTree uf(N);
	sort(begin(G), end(G), compEdge);
	ll res = 0;
	for(int i = 0; i < (int)G.size(); i++){
		edge e = G[i];
		if(not uf.same(e.from, e.to)){
			uf.unite(e.from, e.to);
			res += e.cost;
		}
	}

	cout << res << '\n';

	return 0;
}

Submission Info

Submission Time
Task D - Built?
User yna87
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2387 Byte
Status AC
Exec Time 135 ms
Memory 6132 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 28
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 127 ms 5236 KB
02.txt AC 127 ms 4976 KB
03.txt AC 126 ms 6132 KB
04.txt AC 126 ms 5620 KB
05.txt AC 126 ms 4976 KB
06.txt AC 135 ms 6004 KB
07.txt AC 126 ms 4976 KB
08.txt AC 126 ms 4976 KB
09.txt AC 123 ms 5364 KB
10.txt AC 123 ms 4976 KB
11.txt AC 122 ms 5748 KB
12.txt AC 122 ms 4976 KB
13.txt AC 82 ms 4976 KB
14.txt AC 82 ms 5108 KB
15.txt AC 89 ms 4976 KB
16.txt AC 94 ms 6132 KB
17.txt AC 103 ms 5236 KB
18.txt AC 101 ms 4976 KB
19.txt AC 84 ms 5364 KB
20.txt AC 84 ms 4976 KB
21.txt AC 83 ms 5748 KB
22.txt AC 88 ms 4976 KB
23.txt AC 54 ms 4976 KB
24.txt AC 74 ms 5492 KB
25.txt AC 1 ms 256 KB
26.txt AC 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB