/
BOJ_1867.java
49 lines (48 loc) · 1.54 KB
/
BOJ_1867.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
int[] matched;
boolean[] v;
ArrayList<Integer>[] edges;
private boolean matching(int a) {
for (int b : edges[a]) {
if (v[b]) continue;
v[b] = true;
if (matched[b] == -1 || matching(matched[b])) {
matched[b] = a;
return true;
}
}
return false;
}
private void solution() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
edges = new ArrayList[n+1];
matched = new int[n+1];
Arrays.fill(matched, -1);
for (int i = 1; i <= n; i++) edges[i] = new ArrayList<>();
while (k-->0) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
edges[a].add(b);
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (edges[i].size() == 0) continue;
v = new boolean[n+1];
if (matching(i))
cnt++;
}
System.out.println(cnt);
}
public static void main(String[] args) throws Exception{
new Main().solution();
}
}