Submission #4641921
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
using VS = vector<string>; using LL = long long;
using VI = vector<int>; using VVI = vector<VI>;
using PII = pair<int, int>; using PLL = pair<LL, LL>;
using VL = vector<LL>; using VVL = vector<VL>;
#define ALL(a) begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3")
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x)
#endif
const int INF = 1e9; const LL LINF = 1e16;
const LL MOD = 1000000007; const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };
/* ----- __MAKE_TIME__ Problem: __PROBLEM__ / Link: __CONTEST_URL__ ----- */
/* ------問題------
-----問題ここまで----- */
/* -----解説等-----
----解説ここまで---- */
struct UnionFind {
vector<int> data;
UnionFind(int n) { data.assign(n, -1); }
bool unionSet(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool same(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
LL N; cin >> N;
using tp = tuple<LL, LL, LL>;
vector<tp>xy(N);
FOR(i, 0, N) {
LL x, y; cin >> x >> y;
xy[i] = tp(x, y, i);
}
vector<tp>edge;
SORT(xy); {
FOR(i, 0, N - 1) {
LL a, ai, b, bi;
tie(a, ignore, ai) = xy[i];
tie(b, ignore, bi) = xy[i + 1];
edge.push_back(tp(b - a, ai, bi));
}
}
sort(ALL(xy), [](const tp& a, const tp &b) {return get<1>(a) < get<1>(b); });
{
FOR(i, 0, N - 1) {
LL a, ai, b, bi;
tie(ignore, a, ai) = xy[i];
tie(ignore, b, bi) = xy[i + 1];
edge.push_back(tp(b - a, ai, bi));
}
}
UnionFind uf(N);
SORT(edge);
LL ans = 0;
for (auto it : edge) {
LL c, a, b;
tie(c, a, b) = it;
if (!uf.same(a, b)) {
ans += c;
uf.unionSet(a, b);
}
}
cout << ans << endl;
return 0;
}
Submission Info
| Submission Time |
|
| Task |
D - Built? |
| User |
Yang33 |
| Language |
C++14 (GCC 5.4.1) |
| Score |
500 |
| Code Size |
2480 Byte |
| Status |
AC |
| Exec Time |
63 ms |
| Memory |
10736 KiB |
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 |
62 ms |
9712 KiB |
| 02.txt |
AC |
62 ms |
9328 KiB |
| 03.txt |
AC |
63 ms |
9840 KiB |
| 04.txt |
AC |
62 ms |
9200 KiB |
| 05.txt |
AC |
61 ms |
10096 KiB |
| 06.txt |
AC |
62 ms |
9712 KiB |
| 07.txt |
AC |
62 ms |
9200 KiB |
| 08.txt |
AC |
62 ms |
9584 KiB |
| 09.txt |
AC |
62 ms |
9456 KiB |
| 10.txt |
AC |
61 ms |
10096 KiB |
| 11.txt |
AC |
62 ms |
9328 KiB |
| 12.txt |
AC |
62 ms |
9200 KiB |
| 13.txt |
AC |
53 ms |
9328 KiB |
| 14.txt |
AC |
52 ms |
10736 KiB |
| 15.txt |
AC |
50 ms |
10480 KiB |
| 16.txt |
AC |
55 ms |
9456 KiB |
| 17.txt |
AC |
57 ms |
9200 KiB |
| 18.txt |
AC |
55 ms |
10736 KiB |
| 19.txt |
AC |
51 ms |
9072 KiB |
| 20.txt |
AC |
53 ms |
10352 KiB |
| 21.txt |
AC |
52 ms |
10096 KiB |
| 22.txt |
AC |
56 ms |
9072 KiB |
| 23.txt |
AC |
42 ms |
9584 KiB |
| 24.txt |
AC |
55 ms |
10608 KiB |
| 25.txt |
AC |
1 ms |
256 KiB |
| 26.txt |
AC |
1 ms |
256 KiB |
| s1.txt |
AC |
1 ms |
256 KiB |
| s2.txt |
AC |
1 ms |
256 KiB |