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 Rust (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(dead_code)]

use std::thread;
use std::io::{ self, Read };
use std::fmt;
use std::iter::Iterator;
use std::cmp::*;
use std::collections::*;
use std::str::*;

#[derive(Default)]
struct Input {
    buf: Vec<String>
}

impl Input {
    fn new() -> Input {
        Input { buf: Vec::new() }
    }

    fn read<T: FromStr>(&mut self) -> T 
    where <T as FromStr>::Err: fmt::Debug {
        self.next().unwrap().parse::<T>().unwrap()
    }

    fn read_n<T: FromStr>(&mut self, n: usize) -> Vec<T>
    where <T as FromStr>::Err: fmt::Debug {
        let mut v = Vec::new();
        for _ in 0..n { v.push(self.read::<T>()); }
        v
    }
}

impl Iterator for Input {
    type Item = String;
    fn next(&mut self) -> Option<String> {
        while self.buf.is_empty() {
            let mut s = String::new();
            let _res = io::stdin().read_line(&mut s);
            self.buf = s.split_whitespace().map(|s| s.to_owned()).collect::<Vec<_>>();
            self.buf.reverse();
        }
        self.buf.pop()
    }
}

struct UnionFind {
    c: Vec<(usize, usize)>
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind { c: (0..n).map(|u| (u, 0)).collect::<Vec<_>>() }
    }

    fn root(&self, u: usize) -> usize {
        let v = self.c[u].0;
        if u == v { u } else { self.root(v) }
    }

    fn is_same(&self, u: usize, v: usize) -> bool {
        self.root(u) == self.root(v)
    }

    fn size(&self, u: usize) -> usize {
        self.c[u].1
    }

    fn unite(&mut self, u: usize, v: usize) {
        let ru = self.root(u);
        let rv = self.root(v);
        let su = self.size(u);
        let sv = self.size(v);
        if su > sv {
            self.c[rv] = (ru, su + sv);
        } else {
            self.c[ru] = (rv, su + sv);
        }
    }
}

fn run() {
    let mut ip = Input::new();
    let n = ip.read::<usize>();
    let m = ip.read::<usize>();
    let mut e = Vec::new();
    for _ in 0..m {
        let u = ip.read::<usize>();
        let v = ip.read::<usize>();
        let w = ip.read::<i64>();
        e.push((w, u, v));
    }
    e.sort();
    let mut uf = UnionFind::new(n);
    let mut y = 0i64;
    for (w, u, v) in e {
        if !uf.is_same(u, v) {
            uf.unite(u, v);
            y += w;
        }
    }
    println!("{}", y);
}

fn main() {
    let _ = thread::Builder::new()
        .stack_size(10 * 1024 * 1024)
        .spawn(run).unwrap()
        .join();
}