#K55277. Maximum Reachable Vertex Value
Maximum Reachable Vertex Value
Maximum Reachable Vertex Value
You are given an undirected graph with N vertices and M edges. Each vertex is assigned an initial integer value. Starting from vertex 1, you have to determine the maximum value among all vertices that are reachable from vertex 1.
The graph is provided as follows:
- First, you are given two integers N and M representing the number of vertices and the number of edges respectively.
- Then a line follows containing N integers: the initial values assigned to the vertices (vertex 1 has the first value, vertex 2 the second, and so on).
- Then M lines follow, each containing two integers u and v, indicating that there is an undirected edge between vertex u and vertex v.
Your task is to compute, for each test case, the maximum value of a vertex that can be reached from vertex 1 (including vertex 1 itself).
The connectivity can be determined using a breadth-first search (BFS) or depth-first search (DFS) starting from vertex 1.
inputFormat
The input is read from standard input and has the following format:
T N1 M1 v1 v2 ... vN1 u1 v1 u2 v2 ... (M1 lines for the first test case) N2 M2 v1 v2 ... vN2 ... (M2 lines for the second test case) ...
Here, T indicates the number of test cases. For each test case:
- The first line contains two integers, N (number of vertices) and M (number of edges).
- The second line contains N integers representing the values of vertices 1 to N.
- The next M lines each contain two integers representing an undirected edge connecting two vertices.
outputFormat
For each test case, output a single line containing one integer: the maximum vertex value reachable from vertex 1.
## sample5
5 4
1 5 3 2 6
1 2
2 3
3 4
4 5
3 2
10 20 30
1 2
2 3
4 3
12 7 9 14
1 2
2 3
3 4
3 0
5 8 9
1 0
99
6
30
14
5
99
</p>