From 55319c3457a321152b49f583905d45def479d286 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Thu, 26 Feb 2026 13:59:13 +0900 Subject: [PATCH] =?UTF-8?q?[20260226]=20BOJ=20/=20P3=20/=20=EB=8F=84?= =?UTF-8?q?=EC=8B=9C=20=EC=99=95=EB=B3=B5=ED=95=98=EA=B8=B0=202=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 2.md" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "lkhyun/202602/26 BOJ P3 \353\217\204\354\213\234 \354\231\225\353\263\265\355\225\230\352\270\260 2.md" diff --git "a/lkhyun/202602/26 BOJ P3 \353\217\204\354\213\234 \354\231\225\353\263\265\355\225\230\352\270\260 2.md" "b/lkhyun/202602/26 BOJ P3 \353\217\204\354\213\234 \354\231\225\353\263\265\355\225\230\352\270\260 2.md" new file mode 100644 index 00000000..09675720 --- /dev/null +++ "b/lkhyun/202602/26 BOJ P3 \353\217\204\354\213\234 \354\231\225\353\263\265\355\225\230\352\270\260 2.md" @@ -0,0 +1,88 @@ + ```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; + static final int MAX = Integer.MAX_VALUE / 2; + + 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[2 * N + 1]; + for (int i = 0; i <= 2 * N; i++) adjList[i] = new ArrayList<>(); + edges = new ArrayList<>(); + + for (int i = 1; i <= N; i++) { + int cap = (i == 1 || i == 2) ? MAX : 1; + adjList[i].add(edges.size()); + edges.add(new Edge(i + N, cap)); + adjList[i + N].add(edges.size()); + edges.add(new Edge(i, 0)); + } + + 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 + N].add(edges.size()); + edges.add(new Edge(to, MAX)); + adjList[to].add(edges.size()); + edges.add(new Edge(from + N, 0)); + + adjList[to + N].add(edges.size()); + edges.add(new Edge(from, MAX)); + adjList[from].add(edges.size()); + edges.add(new Edge(to + N, 0)); + } + + int ans = 0; + while (findPath()) ans++; + System.out.println(ans); + } + + public static boolean findPath() { + int[] prev = new int[2 * N + 1]; + Arrays.fill(prev, -1); + prev[1] = -2; + ArrayDeque q = new ArrayDeque<>(); + q.offer(1); + + while (!q.isEmpty()) { + int cur = q.poll(); + for (int i : adjList[cur]) { + int to = edges.get(i).to; + if (prev[to] == -1 && edges.get(i).weight > 0) { + prev[to] = i; + if (to == 2) { + int node = 2; + while (node != 1) { + int idx = prev[node]; + edges.get(idx).weight--; + edges.get(idx ^ 1).weight++; + node = edges.get(idx ^ 1).to; + } + return true; + } + q.offer(to); + } + } + } + return false; + } +} +```