#K52257. Minimum Teleporters to Fix

    ID: 29269 Type: Default 1000ms 256MiB

Minimum Teleporters to Fix

Minimum Teleporters to Fix

You are given a network of n cities and m functioning teleporters connecting pairs of cities. However, some teleporters may be broken. Your task is to determine the minimum number of teleporters that must be fixed to ensure that every city is accessible from every other city.

In graph theoretic terms, the cities are represented as nodes and the functioning teleporters as edges. If the graph has k connected components, you must fix exactly \(k-1\) teleporters to connect all components into one.

Use efficient union-find (disjoint set union) techniques to merge the connected cities and count the number of connected components.

inputFormat

The input is given via standard input (stdin) and has the following format:

  • The first line contains two integers, \(n\) and \(m\): the number of cities and the number of functioning teleporters.
  • The following \(m\) lines each contain two integers, \(u\) and \(v\), indicating a functioning teleporter between city \(u\) and city \(v\).

outputFormat

Output a single integer on standard output (stdout) representing the minimum number of teleporters that must be fixed for all the cities to be connected.

## sample
4 2
1 2
3 4
1

</p>