-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathC.cpp
66 lines (52 loc) · 1.19 KB
/
C.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
struct data {
int person;
int gold;
};
bool compare(data a, data b)
{
return a.gold < b.gold;
}
int main()
{
int n, p;
cin >> n >> p;
data ara[n + 5];
for(int i = 1; i <= n; i++) {
scanf("%d", &ara[i].gold);
ara[i].person = i;
}
map <int, bool> m;
vector <int> v[n + 5];
bool visit[n + 5] = {};
for(int i = 1; i <= p; i++) {
int u, w;
scanf("%d %d", &u, &w);
v[u].push_back(w);
v[w].push_back(u);
}
sort(ara + 1, ara + n + 1, compare);
long long sum = 0;
for(int i = 1; i <= n; i++) {
if(visit[ ara[i].person ] == 1)
continue;
sum += ara[i].gold;
if(v[ ara[i].person ].size() == 0)
continue;
queue <int> st;
st.push(ara[i].person);
while(!st.empty()) {
int u = st.front();
st.pop();
for(int j = 0; j < v[u].size(); j++) {
if(visit[ v[u][j] ] == 0) {
visit[ v[u][j] ] =1;
st.push(v[u][j] );
}
}
}
}
cout << sum << endl;
return 0;
}