int uf[3030]; intfind(int u){ return u == uf[u] ? u : uf[u] = find(uf[u]); } voiduni(int u, int v){ int pu = find(u), pv = find(v); uf[pu] = uf[pv] = min(pu, pv); }
intkruskals(int n, vector<int> f, vector<int> t, vector<int> w){ priority_queue<array<int,3>, vector<array<int,3>>, greater<array<int,3>>> pq; int m = f.size(); for(int i = 1; i <= n; i++ )uf[i] = i; for(int i = 0; i < m; i++) { pq.push({w[i], f[i], t[i]}); } int res = 0; while(!pq.empty()) { auto [w, u, v] = pq.top(); pq.pop(); if(find(u) == find(v)) continue; res += w; uni(u,v); } return res; }