#include #include #include #include #include #include using namespace std; typedef pair PII; typedef vector VI; typedef vector> VII; template void print(vector sez) { for (T x : sez) cout << x << " "; cout << endl; } class DisjointSet { // union-find public: vector parent, size; DisjointSet(int n) { parent = vector(n); size = vector(n); for (int i=0;isize[y]) swap(x,y); // x smaller parent[x]=y; size[y]+=size[x]; } }; bool cmpW(VI e1, VI e2) { return e1[2] < e2[2]; } int Kruskal(int n, vector &edges) { int cost=0; DisjointSet ds(n); sort(edges.begin(),edges.end(),cmpW); for (VI e : edges) { int a=e[0], b=e[1], w=e[2]; if (ds.root(a)==ds.root(b)) continue; cost+=w; ds.join(a,b); } return cost; } int main() { ifstream fin("mst.txt"); int n,m; fin >> n >> m; vector edges; for (int i=0;i> a >> b >> w; edges.push_back({a,b,w}); } // 37 cout << Kruskal(n,edges) << endl; return 0; }