#include #include #include #include #include #include #include #include using namespace std; typedef pair PII; typedef vector VI; typedef vector> VII; typedef vector> VVI; typedef vector>> VVP; class DisjointSet { public: vector parent, size; DisjointSet(int n) { parent = vector(n); size = vector(n); for (int i=0;i &edges, vector &mst) { sort(edges.begin(),edges.end()); DisjointSet ds(n); int cost=0; for (auto e : edges) { int a=e[1], b=e[2], w=e[0]; if (ds.find(a)==ds.find(b)) continue; ds.join(a,b); mst.push_back({a,b}); cost+=w; } return cost; } int main() { ifstream fin("mst.txt"); int n,m; fin >> n >> m; vector edges; for (int i=0;i> a >> b >> c; edges.push_back({c,a,b}); } vector mst; cout << Kruskal(n,edges,mst) << endl; for (auto e : mst) { cout << e.first << " " << e.second << endl; } /* DisjointSet ds(10); ds.join(3,4); ds.join(5,7); ds.join(3,7); for (int i=0;i<10;i++) { cout << i << ": " << ds.find(i) << endl; } */ return 0; }