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 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.read();
    let mut xys = VecVec32::new(n, m);

    for _ in 0..m {
        let (x, y, _z) = i.read::<(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,
        b: Vec<u8>,
    }
    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                b: Vec::new(),
            }
        }
        pub fn read_word(&mut self) -> &[u8] {
            self.b.clear();
            let mut consume = 0;
            loop {
                self.r.consume(consume);
                let b = self.r.fill_buf().unwrap();
                assert!(b.len() != 0);
                if let Some(p) = b.iter().position(|&x| x == b' ' || x == b'\n') {
                    self.b.extend_from_slice(&b[..p]);
                    consume = p + 1;
                    break;
                }
                self.b.extend_from_slice(b);
                consume = b.len();
            }
            self.r.consume(consume);
            &self.b
        }
        pub fn read_word_str(&mut self) -> &str {
            unsafe { std::str::from_utf8_unchecked(self.read_word()) }
        }

        pub fn read<T: CpRead>(&mut self) -> T {
            T::cp_read(self)
        }
    }
    pub trait CpRead {
        fn cp_read<R: BufRead>(r: &mut CpReader<R>) -> Self;
    }

    impl CpRead for u64 {
        fn cp_read<R: BufRead>(r: &mut CpReader<R>) -> Self {
            read_u64_fast(&mut r.r)
        }
    }
    impl CpRead for i64 {
        fn cp_read<R: BufRead>(r: &mut CpReader<R>) -> Self {
            read_i64_fast(&mut r.r)
        }
    }

    macro_rules! cp_read_tuple {
        ($($t:ident),*) => {
            impl<$($t: CpRead),*> CpRead for ($($t),*) {
                fn cp_read<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    ($($t::cp_read(r)),*)
                }
            }
        };
    }

    macro_rules! cp_read_cast {
        ($t_self:ty, $t_read:ty) => {
            impl CpRead for $t_self {
                fn cp_read<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    <$t_read>::cp_read(r) as $t_self
                }
            }
        };
    }
    macro_rules! cp_read_parse {
        ($t:ty) => {
            impl CpRead for $t {
                fn cp_read<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    r.read_word_str().parse().unwrap()
                }
            }
        };
    }


    cp_read_cast!(usize, u64);
    cp_read_cast!(u32, u64);
    cp_read_cast!(u16, u64);
    cp_read_cast!(i32, i64);
    cp_read_cast!(i16, i64);
    cp_read_cast!(i8, i64);

    cp_read_parse!(f64);

    cp_read_tuple!(T1, T2);
    cp_read_tuple!(T1, T2, T3);
    cp_read_tuple!(T1, T2, T3, T4);
    cp_read_tuple!(T1, T2, T3, T4, T5);

    fn read_u64_fast<R: BufRead>(r: &mut R) -> u64 {
        let mut value = 0;
        loop {
            let mut idx = 0;
            let buf_len;
            {
                let buf = r.fill_buf().unwrap();
                buf_len = buf.len();
                assert!(buf_len != 0);
                while idx < buf_len && b'0' <= buf[idx] && buf[idx] <= b'9' {
                    value = value * 10 + (buf[idx] - b'0') as u64;
                    idx += 1;
                }
            }
            if idx < buf_len {
                r.consume(idx + 1);
                return value;
            }
            r.consume(idx);
        }
    }
    fn read_i64_fast<R: BufRead>(r: &mut R) -> i64 {
        let (consume, sign) = match r.fill_buf().unwrap()[0] {
            b'+' => (true, 1),
            b'-' => (true, -1),
            _ => (false, 1),
        };
        if consume {
            r.consume(1);
        }
        read_u64_fast(r) as i64 * sign
    }


    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)
            }
        }
    }
}