#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define llu unsigned llu
#define F first
#define S second
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
vector <iii> g;
vi par;
int fnd(int x){
if (x == par[x])
return x;
par[x] = fnd(par[x]);
return par[x];
}
void onion(int a, int b){
par[fnd(a)] = par[fnd(b)];
}
int main() {
int n, m=0;
cin>>n;
int i, ans = 0;
for (i = 0; i<n; i++)
par.push_back(i);
int a,b,w;
while( cin>>a>>b>>w ){
m++;
g.push_back(iii(w, ii(a,b)));
}
sort(g.begin(), g.end());
for(i = 0; i<m; i++){
if (fnd(g[i].S.F) != fnd(g[i].S.S)){
ans += g[i].F;
onion(g[i].S.F, g[i].S.S);
}
}
cout<<ans<<endl;
return 0;
}
/*
Sample Input:
6
0 1 1
0 3 5
1 3 7
1 2 6
2 5 8
2 4 3
3 4 6
4 5 9
Sample Output:
23
*/
Code Example |
---|
Cpp :: kruskal in c++ |
Cpp :: c++ product of vector |
Cpp :: push front vector cpp |
Cpp :: c++ memory leak |
Cpp :: c++ evaluate expression |
Cpp :: typedef vector c++ |
Cpp :: pow in c++ |
Cpp :: c++ get time |
Cpp :: vector erase specific element |
Cpp :: how to split a string into words c++ |
Cpp :: allow cross origin |
Cpp :: c++ hours minutes seconds |
Cpp :: c++ sleep |
Cpp :: cpp unions |
Cpp :: how to make calculaor in c++ |
Cpp :: slice std::array cpp |
Cpp :: read text from file c++ |
Cpp :: c++ function |
Cpp :: c++ create multidimensional vector |
Cpp :: sleep c++ |
Cpp :: char ascii c++ |
Cpp :: ubuntu dotnet core install |
Cpp :: c++ default parameters |
Cpp :: std distance |
Cpp :: how to find the sum of a vector c++ |
Cpp :: remove from vector by value c++ |
Cpp :: filling 2d array with 0 c++ |
Cpp :: How to write into files in C++ |
Cpp :: to lowercase c++ |
Cpp :: string vector to string c++ |