From d04442838ec04e1336b8be4013f17402dd9d4637 Mon Sep 17 00:00:00 2001 From: HeeEul Shin <83682424+ShinHeeEul@users.noreply.github.com> Date: Wed, 12 Mar 2025 10:19:06 +0900 Subject: [PATCH] =?UTF-8?q?[20250312]=20BOJ=20/=20G3=20/=20=EC=9D=BC?= =?UTF-8?q?=EA=B0=90=ED=98=B8=EC=97=90=20=EB=8B=A4=EB=A6=AC=20=EB=86=93?= =?UTF-8?q?=EA=B8=B0=20/=20=EC=8B=A0=ED=9D=AC=EC=9D=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\353\246\254\353\206\223\352\270\260.md" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "ShinHeeEul/202503/12 BOJ G3 \354\235\274\352\260\220\355\230\270\354\227\220 \353\213\244\353\246\254\353\206\223\352\270\260.md" diff --git "a/ShinHeeEul/202503/12 BOJ G3 \354\235\274\352\260\220\355\230\270\354\227\220 \353\213\244\353\246\254\353\206\223\352\270\260.md" "b/ShinHeeEul/202503/12 BOJ G3 \354\235\274\352\260\220\355\230\270\354\227\220 \353\213\244\353\246\254\353\206\223\352\270\260.md" new file mode 100644 index 00000000..6aed3248 --- /dev/null +++ "b/ShinHeeEul/202503/12 BOJ G3 \354\235\274\352\260\220\355\230\270\354\227\220 \353\213\244\353\246\254\353\206\223\352\270\260.md" @@ -0,0 +1,86 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + + static boolean[] visited; + static int N; + static ArrayList[] lists; + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + long K = Long.parseLong(st.nextToken()); + + visited = new boolean[N + 1]; + + st = new StringTokenizer(br.readLine()); + + PriorityQueue pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]); + + + lists = new ArrayList[N + 1]; + + for(int i = 1; i <= N; i++) pq.add(new Integer[] {i, Integer.parseInt(st.nextToken())}); + for(int i = 1; i < N + 1; i++) lists[i] = new ArrayList<>(); + + + + for(int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + lists[a].add(b); + lists[b].add(a); + + } + + if(M <= 1) { + System.out.println("YES"); + return; + } + + while(!pq.isEmpty()) { + Integer[] node = pq.poll(); + int current = node[0]; + if(visited[current]) continue; + K -= node[1]; + + if(K < 0L) break; + + visited[current] = true; + backTracking(current); + } + + System.out.println(K >= 0L ? "YES" : "NO"); + + } + + public static void backTracking(int current) { + + + int next = current + 1; + if(next == N + 1) next = 1; + int next2 = current - 1; + if(next2 == 0) next2 = N; + + if(!visited[next] && !lists[current].contains(next)) { + visited[next] = true; + backTracking(next); + } + if(!visited[next2] && !lists[current].contains(next2)) { + visited[next2] = true; + backTracking(next2); + } + } + +} +```