Advertisement
qwerty787788

Kuhn algorithm

Dec 21st, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class minimax {
  5.     FastScanner in;
  6.     PrintWriter out;
  7.  
  8.     class Kuhn {
  9.         int n;
  10.         boolean[] used;
  11.         int[] left;
  12.  
  13.         ArrayList<Integer>[] g;
  14.  
  15.         public Kuhn(ArrayList<Integer>[] g) {
  16.             super();
  17.             this.n = g.length;
  18.             this.g = g;
  19.             used = new boolean[n];
  20.             left = new int[n];
  21.         }
  22.  
  23.         boolean tryKuhn(int v) {
  24.             if (used[v])
  25.                 return false;
  26.             used[v] = true;
  27.             for (int i = 0; i < g[v].size(); ++i) {
  28.                 int to = g[v].get(i);
  29.                 if (left[to] == -1 || tryKuhn(left[to])) {
  30.                     left[to] = v;
  31.                     return true;
  32.                 }
  33.             }
  34.             return false;
  35.         }
  36.  
  37.         boolean solve() {
  38.             Arrays.fill(left, -1);
  39.             for (int v = 0; v < n; ++v) {
  40.                 Arrays.fill(used, false);
  41.                 tryKuhn(v);
  42.             }
  43.  
  44.             for (int i = 0; i < n; i++)
  45.                 if (left[i] == -1)
  46.                     return false;
  47.             return true;
  48.         }
  49.  
  50.     }
  51.  
  52.     void solve() {
  53.        
  54.     }
  55.  
  56.     void run() {
  57.         try {
  58.             in = new FastScanner(new File("minimax.in"));
  59.             out = new PrintWriter(new File("minimax.out"));
  60.  
  61.             solve();
  62.  
  63.             out.close();
  64.         } catch (FileNotFoundException e) {
  65.             e.printStackTrace();
  66.         }
  67.     }
  68.  
  69.     void runIO() {
  70.  
  71.         in = new FastScanner(System.in);
  72.         out = new PrintWriter(System.out);
  73.  
  74.         solve();
  75.  
  76.         out.close();
  77.     }
  78.  
  79.     class FastScanner {
  80.         BufferedReader br;
  81.         StringTokenizer st;
  82.  
  83.         public FastScanner(File f) {
  84.             try {
  85.                 br = new BufferedReader(new FileReader(f));
  86.             } catch (FileNotFoundException e) {
  87.                 e.printStackTrace();
  88.             }
  89.         }
  90.  
  91.         public FastScanner(InputStream f) {
  92.             br = new BufferedReader(new InputStreamReader(f));
  93.         }
  94.  
  95.         String next() {
  96.             while (st == null || !st.hasMoreTokens()) {
  97.                 String s = null;
  98.                 try {
  99.                     s = br.readLine();
  100.                 } catch (IOException e) {
  101.                     e.printStackTrace();
  102.                 }
  103.                 if (s == null)
  104.                     return null;
  105.                 st = new StringTokenizer(s);
  106.             }
  107.             return st.nextToken();
  108.         }
  109.  
  110.         boolean hasMoreTokens() {
  111.             while (st == null || !st.hasMoreTokens()) {
  112.                 String s = null;
  113.                 try {
  114.                     s = br.readLine();
  115.                 } catch (IOException e) {
  116.                     e.printStackTrace();
  117.                 }
  118.                 if (s == null)
  119.                     return false;
  120.                 st = new StringTokenizer(s);
  121.             }
  122.             return true;
  123.         }
  124.  
  125.         int nextInt() {
  126.             return Integer.parseInt(next());
  127.         }
  128.  
  129.         long nextLong() {
  130.             return Long.parseLong(next());
  131.         }
  132.     }
  133.  
  134.     public static void main(String[] args) {
  135.         new minimax().run();
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement