#K57222. Minimum Absolute Difference in Graph Components

    ID: 30373 Type: Default 1000ms 256MiB

Minimum Absolute Difference in Graph Components

Minimum Absolute Difference in Graph Components

You are given an undirected graph with \(N\) nodes. Each node is assigned an integer value. The graph also contains \(M\) undirected edges, where each edge connects two nodes.

A pair of nodes is considered connected if they are in the same connected component (i.e. there is a path between them). Your task is to compute the minimum absolute difference between the values of any two nodes that are connected.

If no connected pair exists (for example, when there are no edges or a component contains only one node), output 0.

The absolute difference between two numbers \(a\) and \(b\) is defined as \(|a - b|\).

inputFormat

The input is given via standard input (stdin) and consists of:

  1. An integer \(N\) representing the number of nodes.
  2. \(N\) space-separated integers representing the values assigned to each node.
  3. An integer \(M\) representing the number of edges.
  4. \(M\) lines, each containing two space-separated integers \(u\) and \(v\) which indicate an undirected edge between node \(u\) and node \(v\).

outputFormat

Output a single integer to standard output (stdout): the minimum absolute difference between the values of any two connected nodes. If there is no pair of connected nodes that satisfies the condition, output 0.

## sample
5
4 2 5 9 7
4
1 2
1 3
3 4
4 5
1