#K6136. Minimum Cost to Determine Candy Ranks

    ID: 31292 Type: Default 1000ms 256MiB

Minimum Cost to Determine Candy Ranks

Minimum Cost to Determine Candy Ranks

You are given N candy jars, numbered from 1 to N. There are K conditions provided in the form of queries. Each query is represented as a triplet \( (A_i, B_i, C_i) \), which indicates that jar \( A_i \) is connected to jar \( B_i \) with an associated parameter \( C_i \). Although each connection has an associated value \( C_i \), the cost incurred to determine the candy rank of any jar is a fixed unit cost (i.e. 1 per jar examined).

Starting from jar 1, you may traverse through the jars using the given connections. Your task is to determine the minimum total cost required to ascertain the candy rank in every jar that is reachable from jar 1. In other words, you are to compute the number of jars in the connected component that includes jar 1.

Note: The connections form an undirected graph. The input guarantees that you can traverse from jar to jar using the provided queries, and each visit costs exactly 1 unit.

inputFormat

The input is given via standard input (stdin).

The first line contains two integers N and K, where N is the number of candy jars and K is the number of conditions (queries).

This is followed by K lines, each containing three integers Ai, Bi and Ci separated by spaces, representing a connection between jar Ai and jar Bi with an associated parameter Ci.

It is guaranteed that 1 ≤ Ai, Bi ≤ N.

outputFormat

Output a single integer to standard output (stdout) representing the minimum cost (i.e. the number of jars reachable from jar 1) required to determine the candy ranks.## sample

4 2
1 2 1
3 4 2
2