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 50 51 52 53
| import java.util.*; class Main{ static Map<Integer, List<Integer>> map = new HashMap<>(); static int[] link = null; static boolean[] st = null; public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int e = sc.nextInt(); link = new int[m+1]; st = new boolean[m+1]; for(int i = 0; i < e; i++){ int a = sc.nextInt(); int b = sc.nextInt(); List<Integer> list = map.getOrDefault(a, new ArrayList<>()); if (!list.contains(b)){ list.add(b); } map.put(a, list); }
System.out.println(distribute()); }
public static int distribute(){ int ans = 0; for(Map.Entry<Integer, List<Integer>> entry : map.entrySet()){ if(dfs(entry.getKey())){ Arrays.fill(st, false); ans++; } } return ans; }
public static boolean dfs(int a){ List<Integer> list = map.get(a); for(int i = 0; i < list.size(); i++){ int b = list.get(i); if (st[b]){ continue; } st[b] = true; if(link[b] == 0 || dfs(link[b])){ link[b] = a; return true; } } return false; } }
|