-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon16562.cpp
More file actions
58 lines (48 loc) · 845 Bytes
/
baekjoon16562.cpp
File metadata and controls
58 lines (48 loc) · 845 Bytes
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
#include <iostream>
using namespace std;
int N, M, K, a, b; // 학생수 친구관계수 갖고있는 돈
int cost[10001], parent[10001];
int getParent(int x) {
if (x == parent[x])
return x;
else
return parent[x] = getParent(parent[x]);
}
void unionParent(int a, int b) {
a = getParent(a);
b = getParent(b);
if (a > b) {
parent[a] = b;
}
else
parent[a] = b;
if (cost[a] > cost[b]) {
cost[a] = cost[b];
}
else
cost[b] = cost[a];
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> K;
for (int i = 1; i <= N; i++) {
cin >> cost[i];
parent[i] = i;
}
for (int i = 0; i < M; i++) {
cin >> a >> b;
unionParent(a, b);
}
int answer = 0;
for (int i = 1; i <= N; i++) {
if (parent[i] == i) {
answer += cost[i];
}
}
if (K - answer >= 0)
cout << answer;
else
cout << "Oh no";
}