Skip to content

Commit c6df373

Browse files
authored
[20250620] BOJ / G4 / 친구비 / 김수연
1 parent fd43912 commit c6df373

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class boj16562 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
static int N, M, K, answer;
12+
static int[] parent, money;
13+
public static void main(String[] args) throws Exception {
14+
nextLine();
15+
N = nextInt();
16+
M = nextInt();
17+
K = nextInt();
18+
parent = new int[N+1];
19+
money = new int[N+1];
20+
nextLine();
21+
for (int i = 1; i < N+1; i++) {
22+
parent[i] = i;
23+
money[i] = nextInt();
24+
}
25+
for (int i = 0; i < M; i++) {
26+
nextLine();
27+
int a = nextInt();
28+
int b = nextInt();
29+
union(a, b);
30+
}
31+
for (int i = 1; i < N+1; i++) {
32+
if (find(i) == i) answer += money[i];
33+
}
34+
if (K < answer) System.out.println("Oh no");
35+
else System.out.println(answer);
36+
}
37+
static int find(int x) {
38+
if (x == parent[x]) return x;
39+
return parent[x] = find(parent[x]);
40+
}
41+
42+
static void union(int x, int y) {
43+
x = find(x);
44+
y = find(y);
45+
if (money[x] < money[y]) parent[y] = x;
46+
else parent[x] = y;
47+
}
48+
}
49+
```

0 commit comments

Comments
 (0)