forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlostmap.cpp
65 lines (54 loc) · 1.11 KB
/
lostmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct edge {
int n1;
int n2;
int dist;
};
bool cmp(edge& lhs, edge& rhs) {
return lhs.dist < rhs.dist;
}
int find(vector<int>& d, int a) {
if(d[a] < 0) return a;
return d[a] = find(d, d[a]);
}
void join(vector<int>& d, int a, int b) {
a = find(d, a);
b = find(d, b);
if(a == b) return;
if(d[a] > d[b]) {
swap(a,b);
}
d[a] += d[b];
d[b] = a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int v;
cin >> v;
vector<edge> e;
for(int i = 0; i < v; i++) {
for(int j = 0; j < v; j++) {
int num;
cin >> num;
if(i > j) {
e.push_back({i, j, num});
}
}
}
sort(e.begin(), e.end(), cmp);
vector<int> d(v, -1);
for(auto i : e) {
int n1 = find(d, i.n1);
int n2 = find(d, i.n2);
if(n1 != n2) {
cout << i.n1 + 1 << " " << i.n2 + 1 << endl;
join(d, n1, n2);
}
}
}