int pre[N]; int n, m; structEdge { int u, v; int w; }e[N]; boolcmp(const Edge a, const Edge b){ return a.w < b.w; } voidmake_set(int n){ for(int i = 0; i <= n; i++) pre[i] = i; } intfind_set(int a){ if(pre[a] == a) return a; return pre[a] = find_set(pre[a]); } voidkruskal(){ int sum = 0; sort(e, e + m, cmp); make_set(n); for (int i = 0, fu, fv, cnt_e; i < m; i++) { fu = find_set(e[i].u); fv = find_set(e[i].v); if (fu != fv) { sum += e[i].w; cnt_e++; if (cnt_e == n-1) break; pre[fv] = fu; } } cout << sum << endl; }
Checking if Disqus is accessible...