Problem:
Score : 500 points

Problem Statement

There are N cards placed face down in a row. On each card, an integer 1 or 2 is written.

Let A_i be the integer written on the i-th card.

Your objective is to guess A_1, A_2, ..., A_N correctly.

You know the following facts:

For each i = 1, 2, ..., M, the value A_{X_i} + A_{Y_i} + Z_i is an even number.

You are a magician and can use the following magic any number of times:

Magic: Choose one card and know the integer A_i written on it. The cost of using this magic is 1.

What is the minimum cost required to determine all of A_1, A_2, ..., A_N?

It is guaranteed that there is no contradiction in given input.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq X_i < Y_i \leq N

1 \leq Z_i \leq 100

The pairs (X_i, Y_i) are distinct.

There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.)

Input

Input is given from Standard Input in the following format:

N M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M

Output

Print the minimum total cost required to determine all of A_1, A_2, ..., A_N.

Sample Input 1

3 1
1 2 1

Sample Output 1

2

You can determine all of A_1, A_2, A_3 by using the magic for the first and third cards.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

100000 1
1 100000 100

Sample Output 3

99999

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let (n, m) = i.read2::<usize, usize>();
    let mut xys = VecVec32::new(n, m);

    for _ in 0..m {
        let (x, y, _z) = i.read3::<usize, usize, usize>();
        let (x, y) = (x - 1, y - 1);
        xys.push(x, y as u32);
        xys.push(y, x as u32);
    }
    writeln!(o, "{}", solve(n, &xys)).unwrap();
}
fn solve(n: usize, xys: &VecVec32<u32>) -> usize {
    let mut vs = vec![false; n];
    let mut q = Vec::new();
    let mut ans = 0;
    for i in 0..n {
        if vs[i] {
            continue;
        }
        q.push(i);
        vs[i] = true;
        while let Some(i0) = q.pop() {
            for &i1 in xys.get(i0) {
                let i1 = i1 as usize;
                if !vs[i1] {
                    vs[i1] = true;
                    q.push(i1);
                }
            }
        }
        ans += 1;
    }
    ans
}

mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        { $f:ident($($t:ident),*) } => {
            #[allow(dead_code)]
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                ($(read_next::<$t,_>(i)),*)
            }
        };
    }

    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        fn_read! { read2(T1, T2) }
        fn_read! { read3(T1, T2, T3) }
        fn_read! { read4(T1, T2, T3, T4) }
        fn_read! { read5(T1, T2, T3, T4, T5) }
    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

    pub struct VecVec32<T> {
        hs: Vec<u32>,
        vs: Vec<(T, u32)>,
    }
    #[allow(dead_code)]
    impl<T> VecVec32<T> {
        #[inline]
        pub fn new(len: usize, capacity: usize) -> Self {
            assert!(capacity < u32::max_value() as usize);
            VecVec32 {
                hs: vec![0; len],
                vs: Vec::with_capacity(capacity),
            }
        }

        #[inline]
        pub fn push(&mut self, idx: usize, value: T) {
            self.vs.push((value, self.hs[idx]));
            self.hs[idx] = self.vs.len() as u32;
        }

        #[inline]
        pub fn len(&self) -> usize {
            self.hs.len()
        }

        #[inline]
        pub fn get(&self, idx: usize) -> VecVec32Iter<T> {
            VecVec32Iter {
                s: self,
                i: self.hs[idx],
            }
        }
    }
    pub struct VecVec32Iter<'a, T: 'a> {
        s: &'a VecVec32<T>,
        i: u32,
    }
    impl<'a, T: 'a> Iterator for VecVec32Iter<'a, T> {
        type Item = &'a T;

        #[inline]
        fn next(&mut self) -> Option<Self::Item> {
            if self.i == 0 {
                None
            } else {
                let i = self.i as usize - 1;
                self.i = self.s.vs[i].1;
                Some(&self.s.vs[i].0)
            }
        }
    }
}