Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions khj20006/202602/27 BOJ P3 도시 왕복하기 2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
```java
import java.io.*;
import java.util.*;

public class Main {

static class IOManager {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

private IOManager(){}

static String nextLine() throws Exception {
String line = br.readLine();
st = new StringTokenizer(line);
return line;
}

static String nextToken() throws Exception {
while (!st.hasMoreTokens())
nextLine();
return st.nextToken();
}

static int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}

static long nextLong() throws Exception {
return Long.parseLong(nextToken());
}

static double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}

static void write(String content) throws Exception {
bw.write(content);
}

public static void close() throws Exception {
bw.flush();
bw.close();
br.close();
}
}

//

static final int INF = (int)1e9 + 7;

static int N, P;
static int ans = 0;
static int[][] flow, capacity;
static int[] level;

public static void main(String[] args) throws Exception {
N = IOManager.nextInt();
P = IOManager.nextInt();
flow = new int[2*N+1][2*N+1];
capacity = new int[2*N+1][2*N+1];
for(int i=3;i<=N;i++) {
capacity[i][i+N] = 1;
}
capacity[1][1+N] = capacity[2][2+N] = INF;

for(int i=0;i<P;i++) {
int a = IOManager.nextInt(), b = IOManager.nextInt();
capacity[a+N][b] = 1;
capacity[b+N][a] = 1;
}

while(bfs(1, 2)) {
while(dfs(1, INF, 2) > 0);
}
IOManager.write(ans + "\n");

IOManager.close();
}

public static boolean bfs(int source, int sink) {
level = new int[2*N+1];
Queue<Integer> queue = new ArrayDeque<>();
BitSet visited = new BitSet(2*N+1);
queue.add(source);
visited.set(source);
while(!queue.isEmpty()) {
int cur = queue.poll();
for(int nxt=1;nxt<=2*N;nxt++) {
if(!visited.get(nxt) && capacity[cur][nxt] - flow[cur][nxt] > 0) {
visited.set(nxt);
queue.add(nxt);
level[nxt] = level[cur] + 1;
}
}
}
return visited.get(sink);
}

public static int dfs(int cur, int amount, int sink) {
if(cur == sink) {
ans++;
return amount;
}
for(int nxt=1;nxt<=2*N;nxt++) {
int residual = capacity[cur][nxt] - flow[cur][nxt];
if(residual > 0 && level[nxt] == level[cur] + 1) {
int min = Math.min(amount, residual);
int real = dfs(nxt, min, sink);
if(real > 0) {
flow[cur][nxt] += real;
flow[nxt][cur] -= real;
return real;
}
}
}
return 0;
}
}
```