Problem:
Minimum Spanning Tree

Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).

Input

|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1

, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.

Output

Print the sum of the weights of the Minimum Spanning Tree.

Constraints

1 ≤ |V| ≤ 10,000

0 ≤ |E| ≤ 100,000

0 ≤ wi ≤ 10,000

The graph is connected

There are no parallel edges

There are no self-loops

Sample Input 1

4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5

Sample Output 1

3

Sample Input 2

6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6

Sample Output 2

5

Solution in C++ (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;

class UnionFindForests{
    public:
        vector<ll> parents, ranks;
        UnionFindForests(ll size){
            for (ll i = 0; i < size; ++i)
            {
                parents.push_back(i);
                ranks.push_back(0);
            }
        }

        ll find(ll x){
            if(parents[x] == x){
                return x;
            }else{
                //???????????????????????£?????´???????????´??\??????????????´????????????
                //pass compression(????????§???)
                return parents[x] = find(parents[x]);
            }
        }

        bool same(ll x, ll y){
            if(parents[x] == parents[y]){
                return true;
            }else{
                return false;
            }
        }

        void unite(ll x, ll y){
            ll root_x = find(x);
            ll root_y = find(y);
            if (ranks[root_x] > ranks[root_y])
            {
                parents[root_y] = root_x;
            }else if(ranks[root_x] < ranks[root_y]){
                parents[root_x] = root_y;
            }else if(ranks[root_x] == ranks[root_y]){
                parents[root_x] = root_y;
                ranks[root_y]++;
            }
        }
};

class Edge{ 
public:
	ll source, target, cost;
	
	Edge(ll source = 0, ll target = 0, ll cost = 0)
	:source(source)
	,target(target)
	,cost(cost){}

	static bool compare(const Edge& e1, const Edge& e2){
		return e1.cost < e2.cost;
	}
};

ll V, E;
vector<Edge> edges;

ll kluskal(){
	ll min_cost = 0;
	sort(edges.begin(), edges.end(), Edge::compare);
	UnionFindForests uf = UnionFindForests(V);
	for (int i = 0; i < E; ++i)
	{
		if(uf.same(edges[i].source, edges[i].target) == false){
			uf.unite(edges[i].source, edges[i].target);
			min_cost+= edges[i].cost;
		}
	}
	return min_cost;
}


int main(){
	cin >> V >> E;
	ll source, target, cost;
	for (int i = 0; i < E; ++i)
	{
		cin >> source >> target >> cost;
		edges.push_back(Edge(source, target, cost)); 
	}

	ll ans = kluskal();
	cout << ans << endl;
    return 0;
}