-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSF Problem.cpp
49 lines (46 loc) · 1.13 KB
/
DSF Problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <bits/stdc++.h>
#include<math.h>
#define ll long long int
using namespace std;
ll dfs(vector<vector<ll> > &graph, ll i, vector<bool> &visited, vector<ll> &costs){
//set node as visited
visited[i]=1;
ll c=costs[i];
for(ll j=0;j<graph[i].size();j++){
if(!visited[graph[i][j]]){
// Recursively find minimum cost
c = min(c, dfs(graph, graph[i][j], visited, costs));
}
}
return c;
}
void solve(){
ll n, edges, i, x, y, finalCost=0;
// Vertices and Edges
cin>>n>>edges;
vector<ll> costs;
for(i=0;i<n;i++){
cin>>x;
costs.push_back(x);
}
vector<bool> visited(n, 0);
vector<vector<ll> > graph(n, vector<ll> (0));
for(i=0;i<edges;i++){
cin>>x>>y;
--x;--y;
graph[x].push_back(y);
graph[y].push_back(x);
}
//Find cost for each group and add to result
for(i=0;i<n;i++){
if(!visited[i])
finalCost+=dfs(graph, i, visited, costs);
}
cout<<finalCost;
}
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}