From 6036a336b9998601e32467d41cdb86d557fe8d3b Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Mon, 16 Jun 2025 17:27:58 +0900 Subject: [PATCH] =?UTF-8?q?[20250616]=20BOJ=20/=20G3=20/=20=EC=97=AD?= =?UTF-8?q?=EC=82=AC=20/=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16 BOJ G3 \354\227\255\354\202\254.md" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "suyeun84/202506/16 BOJ G3 \354\227\255\354\202\254.md" diff --git "a/suyeun84/202506/16 BOJ G3 \354\227\255\354\202\254.md" "b/suyeun84/202506/16 BOJ G3 \354\227\255\354\202\254.md" new file mode 100644 index 00000000..f250d31e --- /dev/null +++ "b/suyeun84/202506/16 BOJ G3 \354\227\255\354\202\254.md" @@ -0,0 +1,53 @@ +```java +import java.io.*; +import java.util.*; + +public class boj1613 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + static int N; + static int[][] floyd; + static int INF = 100000000; + public static void main(String[] args) throws Exception { + nextLine(); + N = nextInt(); + int K = nextInt(); + floyd = new int[N+1][N+1]; + for (int i = 0; i < N+1; i++) { + Arrays.fill(floyd[i], INF); + floyd[i][i] = 0; + } + for (int i = 0; i < K; i++) { + nextLine(); + int a = nextInt(); + int b = nextInt(); + floyd[a][b] = 1; + } + solve(); + nextLine(); + int S = nextInt(); + for (int i = 0; i < S; i++) { + nextLine(); + int a = nextInt(); + int b = nextInt(); + if (floyd[a][b] != INF) System.out.println(-1); + else if (floyd[b][a] != INF) System.out.println(1); + else System.out.println(0); + } + } + + static void solve() { + for (int k = 1; k <= N; k++) { + for (int i = 1; i <= N; i++) { + for (int j = 1; j <= N; j++) { + if (floyd[i][k] == INF || floyd[k][j] == INF) continue; + floyd[i][j] = Math.min(floyd[i][j], floyd[i][k]+floyd[k][j]); + } + } + } + } +} +```