-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskals.cpp
More file actions
87 lines (73 loc) · 1.67 KB
/
Copy pathkruskals.cpp
File metadata and controls
87 lines (73 loc) · 1.67 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 <bits/stdc++.h>
using namespace std;
typedef vector<pair<int, pair<int, int>>> graph;
int find(int u, int parent[])
{
if (parent[u] < 0)
return u;
return find(parent[u], parent);
}
void unionByWeight(int u, int v, int parent[])
{
int pu = find(u, parent), pv = find(v, parent);
if (pu != pv)
{
if (parent[pu] > parent[pv])
{
parent[pv] += parent[pu];
parent[pu] = pv;
}
else
{
parent[pu] += parent[pv];
parent[pv] = pu;
}
}
}
graph Kruskals(graph Graph, int V)
{
graph result;
int parent[V];
for (int i = 0; i < V; ++i)
parent[i] = -1;
sort(Graph.begin(), Graph.end());
int E = Graph.size();
for (int i = 0; i < E; ++i)
{
int w = Graph[i].first;
int s = Graph[i].second.first;
int d = Graph[i].second.second;
if (find(s, parent) != find(d, parent))
{
unionByWeight(s, d, parent);
result.push_back(Graph[i]);
}
}
return result;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
graph Graph;
int V, E;
cin >> V >> E;
for (int i = 0; i < E; ++i)
{
int s, d, w;
cin >> s >> d >> w;
Graph.push_back(make_pair(w, make_pair(s, d)));
}
graph result = Kruskals(Graph, V);
int ER = result.size();
int s = 0;
for (int i = 0; i < ER; ++i)
{
s += result[i].first;
cout << result[i].second.first << " " << result[i].second.second << endl;
}
cout << "Weight : " << s << endl;
return 0;
}