#K8546. Scenic Value Aggregation

    ID: 36646 Type: Default 1000ms 256MiB

Scenic Value Aggregation

Scenic Value Aggregation

Problem Statement:

You are given a collection of lakes, each with a scenic value. Additionally, there are pairs of lakes that are connected by tourist preferences, meaning that if a lake is visited, all lakes connected to it (directly or indirectly) must also be visited. These connections form an undirected graph. Your task is to compute the maximum total scenic value that can be obtained by visiting lakes according to the following rule: if a lake is chosen to be visited, then every lake in its connected component (with respect to the preference connections) must also be visited.

Since the connected components are independent, the maximum scenic value for a test case is the sum of the scenic values of all lakes, taking into account the grouping imposed by the preferences.

The input is provided via standard input (stdin) and the output should be printed to standard output (stdout).

inputFormat

The input consists of multiple test cases. The first line contains an integer T, the number of test cases. For each test case, the input format is as follows:

  1. The first line contains two integers N and M, where N is the number of lakes and M is the number of preferences.
  2. The second line contains N space-separated integers representing the scenic values of the lakes (indexed from 1 to N).
  3. Each of the next M lines contains two space-separated integers a and b, indicating that lake a and lake b are connected and must be visited together.

outputFormat

For each test case, print a single integer on a new line representing the maximum total scenic value obtainable by visiting all the lakes while satisfying the given preferences.## sample

2
4 3
1 4 5 3
1 2
2 3
3 4
4 1
10 20 30 40
1 4
13

100

</p>