|
| 1 | +/* |
| 2 | + |
| 3 | + Name: Mehul Chaturvedi |
| 4 | + IIT-Guwahati |
| 5 | + |
| 6 | +*/ |
| 7 | +/* |
| 8 | +This is the story in 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 : |
| 9 | + |
| 10 | +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! |
| 11 | +Given there are N monkeys in the kingdom. Each monkey who wants to team up with another monkey has to perform a ritual. Given total 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. |
| 12 | + |
| 13 | +You are given an array A where Ai is the number of bananas i'th monkey gathers. |
| 14 | + |
| 15 | +Find out the number of gold coins that our Monk should set aside for the prize. |
| 16 | +Input: |
| 17 | +First line contains an long longeger T. T test cases follow. First line of each test case contains two space-separated N and M. M lines follow. Each of the M lines contains two long longegers Xi and Yi, the indexes of monkeys that perform the i'th ritual. Last line of the testcase contains N space-separated long longeger constituting the array A. |
| 18 | +Output: |
| 19 | +Prlong long the answer to each test case in a new line. |
| 20 | +Constralong longs: |
| 21 | +1 ≤ T ≤ 10 |
| 22 | +1 ≤ N ≤ 10^5 |
| 23 | +0 ≤ M ≤ 10^5 |
| 24 | +0 ≤ Ai ≤ 10^12 |
| 25 | +SAMPLE INPUT |
| 26 | +1 |
| 27 | +4 3 |
| 28 | +1 2 |
| 29 | +2 3 |
| 30 | +3 1 |
| 31 | +1 2 3 5 |
| 32 | +SAMPLE OUTPUT |
| 33 | +6 |
| 34 | +Explanation |
| 35 | +Monkeys 1,2 ,3 are in the same team. They gather 1+2+3=6 bananas. |
| 36 | +Monkey 4 is a team. It gathers 5 bananas. |
| 37 | +Therefore, number of gold coins (highest number of bananas by a team) = 6. |
| 38 | +*/ |
| 39 | + |
| 40 | +#include <bits/stdc++.h> |
| 41 | + |
| 42 | +using namespace std; |
| 43 | + |
| 44 | +void dfs(long long sv, vector<long long>* graph, long long n, bool* isVisited, unordered_set<long long>* component){ |
| 45 | + //unordered_set<long long>* ans = new unordered_set<long long>(); |
| 46 | + isVisited[sv] = 1; |
| 47 | + |
| 48 | + component->insert(sv); |
| 49 | + for (long long i = 0; i < graph[sv].size() ; ++i) |
| 50 | + { |
| 51 | + if (isVisited[graph[sv][i]] == 0) |
| 52 | + { |
| 53 | + dfs(graph[sv][i], graph, n, isVisited, component); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return; |
| 58 | +} |
| 59 | + |
| 60 | +unordered_set<unordered_set<long long>*>* connect(vector<long long>* graph, long long n){ |
| 61 | + |
| 62 | + unordered_set<unordered_set<long long>*>* ans = new unordered_set<unordered_set<long long>*>(); |
| 63 | + |
| 64 | + bool* isVisited = new bool[n]; |
| 65 | + for (long long i = 0; i < n; ++i) |
| 66 | + { |
| 67 | + isVisited[i] = 0; |
| 68 | + } |
| 69 | + |
| 70 | + for (long long i = 0; i < n; ++i) |
| 71 | + { |
| 72 | + if (isVisited[i] == 0) |
| 73 | + { |
| 74 | + unordered_set<long long>* component = new unordered_set<long long>(); |
| 75 | + dfs(i, graph, n, isVisited, component); |
| 76 | + ans->insert(component); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return ans; |
| 81 | + |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +long long go(vector<long long>* graph, long long* arr, long long n){ |
| 86 | + unordered_set<unordered_set<long long>*>* components = connect(graph, n); |
| 87 | + |
| 88 | + long long ans = INT_MIN; |
| 89 | + |
| 90 | + auto it1 = components->begin(); |
| 91 | + while(it1!=components->end()){ |
| 92 | + auto it2 = (*it1)->begin(); |
| 93 | + long long temp = 0; |
| 94 | + while(it2!=(*it1)->end()){ |
| 95 | + temp+=arr[*it2]; |
| 96 | + it2++; |
| 97 | + } |
| 98 | + ans = max(temp, ans); |
| 99 | + it1++; |
| 100 | + } |
| 101 | + |
| 102 | + return ans; |
| 103 | +} |
| 104 | + |
| 105 | +int main( int argc , char ** argv ) |
| 106 | +{ |
| 107 | + ios_base::sync_with_stdio(false) ; |
| 108 | + cin.tie(NULL) ; |
| 109 | + |
| 110 | + long long t; |
| 111 | + cin>>t; |
| 112 | + while(t--){ |
| 113 | + long long n, m; |
| 114 | + cin>>n>>m; |
| 115 | + |
| 116 | + vector<long long>* graph = new vector<long long>[n]; |
| 117 | + for (long long i = 0; i < m; ++i) |
| 118 | + { |
| 119 | + long long a, b; |
| 120 | + cin>>a>>b; |
| 121 | + graph[a-1].push_back(b-1); |
| 122 | + graph[b-1].push_back(a-1); |
| 123 | + } |
| 124 | + |
| 125 | + long long* arr = new long long[n]; |
| 126 | + for (long long i = 0; i < n; ++i) |
| 127 | + { |
| 128 | + cin>>arr[i]; |
| 129 | + } |
| 130 | + |
| 131 | + cout << go(graph, arr, n) << '\n'; |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + return 0 ; |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +} |
0 commit comments