# CPP Program Kingdom of Monkeys – Advanced Graphs

• This is the story of Zimbo, the kingdom officially made for monkeys. Our Code Monk visited Zimbo and declared open a challenge in the kingdom, thus spoke to all the monkeys :
• You all have to make teams and go on a hunt for Bananas. The team that returns with the highest number of Bananas will be rewarded with as many gold coins as the number of Bananas with them. May the force be with you!
• Given there are N monkeys in the kingdom. Each monkey who wants to team up with another monkey has to perform a ritual. Given a total of M rituals are performed. Each ritual teams up two monkeys. If Monkeys A and B teamed up and Monkeys B and C teamed up, then Monkeys A and C are also in the same team.
• You are given an array A where Ai is the number of bananas i’th monkey gathers.
• Find out the number of gold coins that our Monk should set aside for the prize.

Input:

The first line contains an integer T. T test cases follow. The first line of each test case contains two space-separated N and M. M lines that follow. Each of the M lines contains two integers Xi and Yi, the indexes of monkeys that perform the i’th ritual. The last line of the test case contains N space-separated integers constituting the array A.

Output:

• Print the answer to each test case in a new line.

Constraints:

1 ≤ T ≤ 10
1 ≤ N ≤ 10^5
0 ≤ M ≤ 10^5
0 ≤ Ai ≤ 10^12

Sample Input:

```1
4 3
1 2
2 3
3 1
1 2 3 5```

Sample Output:

```.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
`6````

Explanation:

• Monkeys 1, 2, and 3 are in the same team. They gather 1+2+3=6 bananas.
• Monkey 4 is a team. It gathers 5 bananas.
• Therefore, the number of gold coins (highest number of bananas by a team) = 6.

## Code:

``````#include<iostream>
#include<vector>
#include<climits>
using namespace std;
typedef long long int ll;
ll dfs(vector<ll>* edges, ll* arr, ll n, bool* visited, ll start)
{
ll sum = arr[start];
visited[start] = true;
for (ll i = 0; i < edges[start].size(); i++)
{
{
sum += dfs(edges, arr, n, visited, adjacent);
}
}
return sum;
}
int main()
{
ll t;
cin >> t;
while (t--)
{
ll n, m;
cin >> n >> m;
vector<ll>* edges = new vector<ll>[n];
for (ll i = 0; i < m; i++)
{
ll x, y;
cin >> x >> y;
edges[x - 1].push_back(y - 1);
edges[y - 1].push_back(x - 1);
}
ll* arr = new ll[n];
for (ll i = 0; i < n; i++)
{
cin >> arr[i];
}
bool* visited = new bool[n];
for (ll i = 0; i < n; i++)
{
visited[i] = false;
}
ll maximum = INT_MIN;
for (ll i = 0; i < n; i++)
{
if (!visited[i])
{
ll current_component_total_bananas = dfs(edges, arr, n, visited, i);
if (current_component_total_bananas > maximum)
{
maximum = current_component_total_bananas;
}
}
}
cout << maximum << endl;
}
}
```Code language: C++ (cpp)```