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; + } +} +```