From f8e2a7b5c2a14da1f1159264ac3533d156b49f72 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Wed, 25 Feb 2026 14:36:05 +0900 Subject: [PATCH] =?UTF-8?q?[20260225]=20BOJ=20/=20P4=20/=20=EB=8F=84?= =?UTF-8?q?=EC=8B=9C=20=EC=99=95=EB=B3=B5=ED=95=98=EA=B8=B0=201=20/=20?= =?UTF-8?q?=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\263\265\355\225\230\352\270\260 1.md" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "lkhyun/202602/25 P4 \353\217\204\354\213\234 \354\231\225\353\263\265\355\225\230\352\270\260 1.md" diff --git "a/lkhyun/202602/25 P4 \353\217\204\354\213\234 \354\231\225\353\263\265\355\225\230\352\270\260 1.md" "b/lkhyun/202602/25 P4 \353\217\204\354\213\234 \354\231\225\353\263\265\355\225\230\352\270\260 1.md" new file mode 100644 index 00000000..c0c555e3 --- /dev/null +++ "b/lkhyun/202602/25 P4 \353\217\204\354\213\234 \354\231\225\353\263\265\355\225\230\352\270\260 1.md" @@ -0,0 +1,74 @@ +```java +import java.util.*; +import java.io.*; + +public class Main { + static class Edge { + int to, weight; + public Edge(int to, int weight) { + this.to = to; + this.weight = weight; + } + } + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static int N, P; + static List[] adjList; + static List edges = new ArrayList<>(); + static int ans = 0; + + public static void main(String[] args) throws Exception { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + P = Integer.parseInt(st.nextToken()); + adjList = new List[N + 1]; + for (int i = 0; i <= N; i++) { + adjList[i] = new ArrayList<>(); + } + + for (int i = 0; i < P; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + adjList[from].add(edges.size()); + edges.add(new Edge(to, 1)); + adjList[to].add(edges.size()); + edges.add(new Edge(from, 0)); + } + + while (findPath()) ans++; + System.out.println(ans); + } + + public static boolean findPath() { + int[] prev = new int[N+1]; + Arrays.fill(prev, -1); + ArrayDeque q = new ArrayDeque<>(); + q.offer(1); + prev[1] = 0; + + while(!q.isEmpty()){ + int cur = q.poll(); + if(cur == 2) break; + for(int i : adjList[cur]){ + int next = edges.get(i).to; + if(prev[next] == -1 && edges.get(i).weight > 0){ + prev[next] = i; + q.offer(next); + } + } + } + + if(prev[2] == -1) return false; + + int cur = 2; + while(cur != 1){ + int idx = prev[cur]; + edges.get(idx).weight--; + edges.get(idx ^ 1).weight++; + cur = edges.get(idx ^ 1).to; + } + return true; + } +} +```