Submission #11471225
Source Code Expand
#ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; int n; const int maxN = 1e5 + 10; int x[maxN], y[maxN]; int p[maxN]; int find(int a) { if (a == p[a]) return a; return p[a] = find(p[a]); } bool unite(int a, int b) { a = find(a); b = find(b); if (a == b) return false; if (rand() & 1) swap(a, b); p[a] = b; return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // freopen("input.txt", "r", stdin); cin >> n; vector < pair < int, int > > byX, byY; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i]; byX.emplace_back(x[i], i); byY.emplace_back(y[i], i); } sort(byX.begin(), byX.end()); sort(byY.begin(), byY.end()); vector < pair < int, pair < int, int > > > edges; for (int i = 0; i + 1 < n; i++) { edges.emplace_back(byX[i + 1].first - byX[i].first, make_pair(byX[i].second, byX[i + 1].second)); edges.emplace_back(byY[i + 1].first - byY[i].first, make_pair(byY[i].second, byY[i + 1].second)); } sort(edges.begin(), edges.end()); ll ans = 0; for (int i = 1; i <= n; i++) p[i] = i; for (auto it : edges) { if (unite(it.second.first, it.second.second)) ans += it.first; } cout << ans; return 0; }
Submission Info
Submission Time | |
---|---|
Task | D - Built? |
User | progmatic |
Language | C++14 (GCC 5.4.1) |
Score | 500 |
Code Size | 1440 Byte |
Status | AC |
Exec Time | 67 ms |
Memory | 7912 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 | 66 ms | 7656 KB |
02.txt | AC | 67 ms | 7144 KB |
03.txt | AC | 67 ms | 6504 KB |
04.txt | AC | 67 ms | 6504 KB |
05.txt | AC | 66 ms | 7016 KB |
06.txt | AC | 66 ms | 7272 KB |
07.txt | AC | 67 ms | 7656 KB |
08.txt | AC | 67 ms | 6504 KB |
09.txt | AC | 65 ms | 7400 KB |
10.txt | AC | 65 ms | 7400 KB |
11.txt | AC | 65 ms | 6504 KB |
12.txt | AC | 65 ms | 6504 KB |
13.txt | AC | 49 ms | 6504 KB |
14.txt | AC | 49 ms | 6504 KB |
15.txt | AC | 56 ms | 7912 KB |
16.txt | AC | 63 ms | 6504 KB |
17.txt | AC | 64 ms | 7528 KB |
18.txt | AC | 59 ms | 7528 KB |
19.txt | AC | 56 ms | 7144 KB |
20.txt | AC | 60 ms | 6504 KB |
21.txt | AC | 60 ms | 6504 KB |
22.txt | AC | 62 ms | 7016 KB |
23.txt | AC | 33 ms | 7272 KB |
24.txt | AC | 62 ms | 6504 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 |