-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathioimaintain.cpp
More file actions
87 lines (69 loc) · 1.51 KB
/
ioimaintain.cpp
File metadata and controls
87 lines (69 loc) · 1.51 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX_N = 100010;
int n,w;
vector<int> adj[MAX_N];
vector<int> weights[MAX_N];
vector<pair<int,int>> edges;
vector<int> edge_weights;
vector<int> edge_status;
set<pair<int,int>> Q;
vector<pair<int,int>> sedges;
int seen[MAX_N];
int set_number[MAX_N];
void init_set(){
for(int i=0; i<n; i++){
set_number[i] = i;
seen[i] = 0;
}
}
int set_find(int u){
return set_number[u];
}
void set_union(int pu, int pv){
for(int i=0; i<n; i++)
if(set_number[i] == pu) set_number[i] = pv;
}
int main(){
cin >> n >> w;
for(int i = 0; i < w; i++) {
int a, b, w;
cin >> a >> b >> w; a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
weights[a].push_back(w);
weights[b].push_back(w);
edges.push_back(make_pair(a,b));
edge_weights.push_back(w);
sedges.push_back(make_pair(w,i));
int total = 0;
init_set();
sort(sedges.begin(), sedges.end());
for(int j = 0; j < n; j++) {
int e = sedges[j].second;
int u = edges[e].first;
int v = edges[e].second;
int we = edge_weights[e];
int pu = set_find(u);
int pv = set_find(v);
if(pu != pv) {
total += we;
set_union(pu,pv);
}
}
bool mst = true;
for(int j = 1; j < n; j++){
if(set_find(j) != set_find(0)){
mst = false;
break;
}
}
if(mst) cout << total << "\n";
else cout << "-1\n";
}
return 0;
}