Skip to content

Commit 9a941cf

Browse files
authored
Merge pull request #232 from AlgorithmWithGod/ShinHeeEul
[20250312] BOJ / G3 / 일감호에 다리 놓기 / 신희을
2 parents 14c1d6f + d044428 commit 9a941cf

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
8+
static boolean[] visited;
9+
static int N;
10+
static ArrayList<Integer>[] lists;
11+
12+
public static void main(String[] args) throws Exception{
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
N = Integer.parseInt(st.nextToken());
18+
int M = Integer.parseInt(st.nextToken());
19+
long K = Long.parseLong(st.nextToken());
20+
21+
visited = new boolean[N + 1];
22+
23+
st = new StringTokenizer(br.readLine());
24+
25+
PriorityQueue<Integer[]> pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]);
26+
27+
28+
lists = new ArrayList[N + 1];
29+
30+
for(int i = 1; i <= N; i++) pq.add(new Integer[] {i, Integer.parseInt(st.nextToken())});
31+
for(int i = 1; i < N + 1; i++) lists[i] = new ArrayList<>();
32+
33+
34+
35+
for(int i = 0; i < M; i++) {
36+
st = new StringTokenizer(br.readLine());
37+
38+
int a = Integer.parseInt(st.nextToken());
39+
int b = Integer.parseInt(st.nextToken());
40+
41+
lists[a].add(b);
42+
lists[b].add(a);
43+
44+
}
45+
46+
if(M <= 1) {
47+
System.out.println("YES");
48+
return;
49+
}
50+
51+
while(!pq.isEmpty()) {
52+
Integer[] node = pq.poll();
53+
int current = node[0];
54+
if(visited[current]) continue;
55+
K -= node[1];
56+
57+
if(K < 0L) break;
58+
59+
visited[current] = true;
60+
backTracking(current);
61+
}
62+
63+
System.out.println(K >= 0L ? "YES" : "NO");
64+
65+
}
66+
67+
public static void backTracking(int current) {
68+
69+
70+
int next = current + 1;
71+
if(next == N + 1) next = 1;
72+
int next2 = current - 1;
73+
if(next2 == 0) next2 = N;
74+
75+
if(!visited[next] && !lists[current].contains(next)) {
76+
visited[next] = true;
77+
backTracking(next);
78+
}
79+
if(!visited[next2] && !lists[current].contains(next2)) {
80+
visited[next2] = true;
81+
backTracking(next2);
82+
}
83+
}
84+
85+
}
86+
```

0 commit comments

Comments
 (0)