Submission #3817009
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;
class UF {
vector<int> par; // 親
vector<int> rank; // 深さ
public:
void init(int n) { // n個で初期化
for(int i=0;i<n;i++) {
par.push_back(i);
rank.push_back(0);
}
}
UF(int n) {
for(int i=0;i<n;i++) {
par.push_back(i);
rank.push_back(0);
}
}
int find(int x) { // 木の中のxの深さを返す
if (par[x] == x) return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y) { // xとyを統合する
x = find(x);
y = find(y);
if (x == y) return;
if(rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (rank[x] == rank[y]) rank[x]++;
}
}
bool same(int x, int y) { // xとyが等しいかどうか判定する
return find(x) == find(y);
}
};
struct Edge {
int from, to, cost;
};
bool operator<(const Edge& e1, const Edge& e2) {
return e1.cost > e2.cost;
}
struct Point {
int pos, x, y;
};
bool operator<(const Point& p1, const Point& p2) {
return p1.x < p2.x;
}
bool operator>(const Point& p1, const Point& p2) {
return p1.y < p2.y;
}
template<typename T>
void printv(vector<T> const& s) {
REP(i, SZ(s)) {
cout << s[i] << " ";
}
cout << endl;
}
int main () {
cin.tie(0);
cout << setprecision(10);
int n; cin >> n;
vector<Point> p(n);
REP(i, n) {
p[i].pos = i;
cin >> p[i].x >> p[i].y;
}
vector<vector<Edge>> g(n);
sort(ALL(p));
REP(i, n-1) {
Edge e;
int cost = min(abs(p[i].x - p[i+1].x), abs(p[i].y - p[i+1].y));
e.from = p[i].pos;
e.to = p[i+1].pos;
e.cost = cost;
g[e.from].pb(e);
swap(e.from, e.to);
g[e.from].pb(e);
}
sort(ALL(p), greater<Point>());
REP(i, n-1) {
Edge e;
int cost = min(abs(p[i].x - p[i+1].x), abs(p[i].y - p[i+1].y));
e.from = p[i].pos;
e.to = p[i+1].pos;
e.cost = cost;
g[e.from].pb(e);
swap(e.from, e.to);
g[e.from].pb(e);
}
UF uf(n);
ll ans = 0;
int cnt = 0;
priority_queue<Edge> edges;
REP(i, SZ(g[0])) {
edges.push(g[0][i]);
}
while(cnt < n-1) {
Edge next = edges.top(); edges.pop();
if(!uf.same(next.from, next.to)) {
uf.unite(next.from, next.to);
ans += next.cost;
cnt++;
REP(i, SZ(g[next.to])) {
edges.push(g[next.to][i]);
}
}
}
cout << ans << endl;
}
Submission Info
Submission Time |
|
Task |
D - Built? |
User |
kanra824 |
Language |
C++14 (GCC 5.4.1) |
Score |
500 |
Code Size |
3203 Byte |
Status |
AC |
Exec Time |
198 ms |
Memory |
16152 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 |
188 ms |
14748 KB |
02.txt |
AC |
191 ms |
14744 KB |
03.txt |
AC |
198 ms |
15260 KB |
04.txt |
AC |
186 ms |
15644 KB |
05.txt |
AC |
187 ms |
14744 KB |
06.txt |
AC |
192 ms |
16152 KB |
07.txt |
AC |
190 ms |
14744 KB |
08.txt |
AC |
192 ms |
15384 KB |
09.txt |
AC |
189 ms |
15900 KB |
10.txt |
AC |
185 ms |
16028 KB |
11.txt |
AC |
177 ms |
14744 KB |
12.txt |
AC |
171 ms |
14748 KB |
13.txt |
AC |
130 ms |
14264 KB |
14.txt |
AC |
134 ms |
15416 KB |
15.txt |
AC |
100 ms |
10968 KB |
16.txt |
AC |
110 ms |
10968 KB |
17.txt |
AC |
121 ms |
10968 KB |
18.txt |
AC |
125 ms |
10968 KB |
19.txt |
AC |
101 ms |
10968 KB |
20.txt |
AC |
123 ms |
12748 KB |
21.txt |
AC |
102 ms |
10968 KB |
22.txt |
AC |
110 ms |
10968 KB |
23.txt |
AC |
96 ms |
14264 KB |
24.txt |
AC |
124 ms |
14616 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 |