Skip to content

Commit 3e0fc1c

Browse files
authored
[20260227] BOJ / P3 / 도시 왕복하기 2 / 권혁준
1 parent 4d6703c commit 3e0fc1c

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static class IOManager {
8+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
static StringTokenizer st = new StringTokenizer("");
11+
12+
private IOManager(){}
13+
14+
static String nextLine() throws Exception {
15+
String line = br.readLine();
16+
st = new StringTokenizer(line);
17+
return line;
18+
}
19+
20+
static String nextToken() throws Exception {
21+
while (!st.hasMoreTokens())
22+
nextLine();
23+
return st.nextToken();
24+
}
25+
26+
static int nextInt() throws Exception {
27+
return Integer.parseInt(nextToken());
28+
}
29+
30+
static long nextLong() throws Exception {
31+
return Long.parseLong(nextToken());
32+
}
33+
34+
static double nextDouble() throws Exception {
35+
return Double.parseDouble(nextToken());
36+
}
37+
38+
static void write(String content) throws Exception {
39+
bw.write(content);
40+
}
41+
42+
public static void close() throws Exception {
43+
bw.flush();
44+
bw.close();
45+
br.close();
46+
}
47+
}
48+
49+
//
50+
51+
static final int INF = (int)1e9 + 7;
52+
53+
static int N, P;
54+
static int ans = 0;
55+
static int[][] flow, capacity;
56+
static int[] level;
57+
58+
public static void main(String[] args) throws Exception {
59+
N = IOManager.nextInt();
60+
P = IOManager.nextInt();
61+
flow = new int[2*N+1][2*N+1];
62+
capacity = new int[2*N+1][2*N+1];
63+
for(int i=3;i<=N;i++) {
64+
capacity[i][i+N] = 1;
65+
}
66+
capacity[1][1+N] = capacity[2][2+N] = INF;
67+
68+
for(int i=0;i<P;i++) {
69+
int a = IOManager.nextInt(), b = IOManager.nextInt();
70+
capacity[a+N][b] = 1;
71+
capacity[b+N][a] = 1;
72+
}
73+
74+
while(bfs(1, 2)) {
75+
while(dfs(1, INF, 2) > 0);
76+
}
77+
IOManager.write(ans + "\n");
78+
79+
IOManager.close();
80+
}
81+
82+
public static boolean bfs(int source, int sink) {
83+
level = new int[2*N+1];
84+
Queue<Integer> queue = new ArrayDeque<>();
85+
BitSet visited = new BitSet(2*N+1);
86+
queue.add(source);
87+
visited.set(source);
88+
while(!queue.isEmpty()) {
89+
int cur = queue.poll();
90+
for(int nxt=1;nxt<=2*N;nxt++) {
91+
if(!visited.get(nxt) && capacity[cur][nxt] - flow[cur][nxt] > 0) {
92+
visited.set(nxt);
93+
queue.add(nxt);
94+
level[nxt] = level[cur] + 1;
95+
}
96+
}
97+
}
98+
return visited.get(sink);
99+
}
100+
101+
public static int dfs(int cur, int amount, int sink) {
102+
if(cur == sink) {
103+
ans++;
104+
return amount;
105+
}
106+
for(int nxt=1;nxt<=2*N;nxt++) {
107+
int residual = capacity[cur][nxt] - flow[cur][nxt];
108+
if(residual > 0 && level[nxt] == level[cur] + 1) {
109+
int min = Math.min(amount, residual);
110+
int real = dfs(nxt, min, sink);
111+
if(real > 0) {
112+
flow[cur][nxt] += real;
113+
flow[nxt][cur] -= real;
114+
return real;
115+
}
116+
}
117+
}
118+
return 0;
119+
}
120+
}
121+
```

0 commit comments

Comments
 (0)