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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System; 
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Linq; 
using E = System.Linq.Enumerable; 


class Program {
   
    static void Main(string[] args) {
        var AB = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
        var N = AB[0];
        var M = AB[1];
        
        var UF = new UnionFind(N + 1);
        for(int i = 0;i < M;i++){
            var XYZ = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            var X = XYZ[0];
            var Y = XYZ[1];
            var Z = XYZ[2];
            
            UF.Union(X,Y);
        }
        
        var GroupInfo = new bool[N+1];
        for(int i = 1;i <= N;i++){
            GroupInfo[UF.Find(i)] = true;     
        }
        
        var count = 0;
        for(int i = 1;i <= N;i++){
            if(GroupInfo[i]){
                count++;
            }
        }
        Console.WriteLine(count);
     }
}
     
class UnionFind

 

{

 

// 親要素のインデックスを保持する

 

// 親要素が存在しない(自身がルートである)とき、マイナスでグループの要素数を持つ

 

public long[] Parents { get; set; }

 

public UnionFind(long n)

 

{

 

this.Parents = new long[n];

 

for (int i = 0; i < n; i++)

 

{

 

// 初期状態ではそれぞれが別のグループ(ルートは自分自身)

 

// ルートなのでマイナスで要素数(1個)を保持する

 

this.Parents[i] = -1;

 

}

 

}

 

 

 

// 要素xのルート要素はどれか

 

public long Find(long x)

 

{

 

// 親がマイナスの場合は自分自身がルート

 

if (this.Parents[x] < 0) return x;

 

// ルートが見つかるまで再帰的に探す

 

// 見つかったルートにつなぎかえる

 

this.Parents[x] = Find(this.Parents[x]);

 

return this.Parents[x];

 

}

 

 

 

// 要素xの属するグループの要素数を取得する

 

public long Size(long x)

 

{

 

// ルート要素を取得して、サイズを取得して返す

 

return -this.Parents[this.Find(x)];

 

}

 

 

 

// 要素x, yが同じグループかどうか判定する

 

public bool Same(long x, long y)

 

{

 

return this.Find(x) == this.Find(y);

 

}

 

 

 

// 要素x, yが属するグループを同じグループにまとめる

 

public bool Union(long x, long y)

 

{

 

// x, y のルート

 

x = this.Find(x);

 

y = this.Find(y);

 

// すでに同じグループの場合処理しない

 

if (x == y) return false;

 

 

 

// 要素数が少ないグループを多いほうに書き換えたい

 

if (this.Size(x) < this.Size(y))

 

{

 

var tmp = x;

 

x = y;

 

y = tmp;

 

}

 

// まとめる先のグループの要素数を更新

 

this.Parents[x] += this.Parents[y];

 

// まとめられるグループのルートの親を書き換え

 

this.Parents[y] = x;

 

return true;

 

}

 

}





     
                   
public class PriorityQueue<T> : IEnumerable<T> { private readonly List<T> _data = new List<T>(); private readonly IComparer<T> _comparer; private readonly bool _isDescending; public PriorityQueue(IComparer<T> comparer, bool isDescending = true) { _comparer = comparer; _isDescending = isDescending; } public PriorityQueue(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) { } public PriorityQueue(bool isDescending = true) : this(Comparer<T>.Default, isDescending) { } public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); }