Submission #2530881
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define RFOR(i,a,b) for(int i=(int)b-1;i>=(int)a;--i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define debug(x) cerr << #x << ":" << x << endl;
#define OK(ok) cout << (ok ? "Yes" : "No") << endl;
typedef long long ll;
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail) {
cin >> head; CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 1;
int N;
struct Edge {
Edge(int _from, int _to, int _cost)
: from(_from), to(_to), cost(_cost){}
int from;
int to;
int cost;
};
vector< Edge > e;
bool state[MAX_N];
struct UnionFind {
vector< int > par;
UnionFind(int n = 1) {
init(n);
}
void init(int n) {
par.resize(n);
for(int i = 0; i < n; i++)
par[i] = -1;
}
int root(int n) {
if (par[n] < 0) return n;
return par[n] = root(par[n]);
}
bool unite(int x, int y) {
x = root(x); y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
};
int kruscal()
{
sort(ALL(e), [](Edge x, Edge y) {
return x.cost < y.cost;
});
UnionFind uf(N);
int res = 0;
EACH(ei, e) {
// if (state[ei.from] && state[ei.to]) continue;
if (uf.root(ei.from) != uf.root(ei.to)){
uf.unite(ei.from, ei.to);
state[ei.from] = state[ei.to] = true;
res += ei.cost;
}
}
return res;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
// x, y, i
vector< tuple< int, int, int > > x(N);
// y, x, i
vector< tuple< int, int, int > > y(N);
REP(i, N) {
CIN(p, q);
x[i] = make_tuple(p, q, i);
y[i] = make_tuple(q, p, i);
}
sort(ALL(x));
sort(ALL(y));
REP(i, N - 1) {
// x
int tmp1, tmp2, from, to, cost;
tie(tmp1, tmp2, from) = x[i];
tie(tmp2, cost, to) = x[i + 1];
e.emplace_back(Edge(from, to, abs(tmp2 - tmp1)));
}
REP(i, N) {
// y
int tmp1, tmp2, from, to, cost;
tie(tmp1, tmp2, from) = y[i];
tie(tmp2, cost, to) = y[i + 1];
e.emplace_back(Edge(from, to, abs(tmp2 - tmp1)));
}
int ans = kruscal();
cout << ans << endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Built? |
User |
task4233 |
Language |
C++14 (GCC 5.4.1) |
Score |
500 |
Code Size |
2666 Byte |
Status |
AC |
Exec Time |
57 ms |
Memory |
7284 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
500 / 500 |
Status |
|
|
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 |
56 ms |
5876 KB |
02.txt |
AC |
56 ms |
7284 KB |
03.txt |
AC |
57 ms |
7156 KB |
04.txt |
AC |
56 ms |
5876 KB |
05.txt |
AC |
56 ms |
6772 KB |
06.txt |
AC |
56 ms |
6516 KB |
07.txt |
AC |
56 ms |
5876 KB |
08.txt |
AC |
56 ms |
6388 KB |
09.txt |
AC |
55 ms |
5876 KB |
10.txt |
AC |
55 ms |
5876 KB |
11.txt |
AC |
55 ms |
7028 KB |
12.txt |
AC |
55 ms |
6388 KB |
13.txt |
AC |
48 ms |
5876 KB |
14.txt |
AC |
48 ms |
6772 KB |
15.txt |
AC |
40 ms |
7028 KB |
16.txt |
AC |
43 ms |
5876 KB |
17.txt |
AC |
44 ms |
6644 KB |
18.txt |
AC |
43 ms |
7156 KB |
19.txt |
AC |
42 ms |
5876 KB |
20.txt |
AC |
42 ms |
6644 KB |
21.txt |
AC |
42 ms |
6772 KB |
22.txt |
AC |
44 ms |
5876 KB |
23.txt |
AC |
31 ms |
5876 KB |
24.txt |
AC |
42 ms |
5876 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 |