#include #include #include #include #include #include using namespace std; typedef pair PII; 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]; } }; int Prim(vector> &adj) { int n=adj.size(); vector dist(n,-1); vector done(n); priority_queue, greater> pq; // min-heap int cost=0; dist[0]=0; pq.push({0,0}); while (!pq.empty()) { auto [d,x]=pq.top(); pq.pop(); if (done[x]) continue; // ignore old values in pq cost+=d; done[x]=1; for (auto [y,w] : adj[x]) if (!done[y]) { if (dist[y]==-1 || w < dist[y]) { dist[y]=w; pq.push({w,y}); } } } return cost; } int main() { /* DisjointSet ds(10); ds.join(3,4); ds.join(5,6); ds.join(3,6); ds.join(2,7); cout << (ds.root(4) == ds.root(7)) << endl; cout << (ds.root(3) == ds.root(5)) << endl; */ ifstream fin("mst.txt"); int n,m; fin >> n >> m; vector> adj(n); for (int i=0;i> a >> b >> w; adj[a].push_back({b,w}); adj[b].push_back({a,w}); } //for (auto [x,w] : adj[7]) cout << x << " " << w << endl; cout << Prim(adj) << endl; return 0; }