-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
59 lines (48 loc) Β· 1.68 KB
/
Main.java
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
package Graph.P16562;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int N, M, k, cost = 0;
static int[] A, root;
static PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> A[o1] - A[o2]);
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Graph/P16562/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
A = new int[N];
root = new int[N+1];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
A[i] = Integer.parseInt(st.nextToken());
root[i] = i;
pq.offer(i);
}
root[N] = N;
while (M-- > 0) {
st = new StringTokenizer(br.readLine());
int v = Integer.parseInt(st.nextToken())-1, w = Integer.parseInt(st.nextToken())-1;
merge(v, w);
}
while (!pq.isEmpty()) {
int i = pq.poll();
if (find(i) == find(N)) continue;
merge(i, N);
cost += A[i];
}
if (k < cost) System.out.println("Oh no");
else System.out.println(cost);
}
static int find(int n) {
if (root[n] == n) return n;
return root[n] = find(root[n]);
}
static void merge(int a, int b) {
root[find(b)] = find(a);
}
}