UVA 10919 – Prerequisites?

Super easy. HashSet would have provided better performance than an ArrayList though.

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P10919 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    while (sc.hasNextInt()) {
      int K = sc.nextInt();
      if (K == 0)
        break;
      int M = sc.nextInt();

      ArrayList<String> courses = new ArrayList<>();
      for (int i = 0; i < K; i++) {
        courses.add(sc.next());
      }

      boolean good = true;
      for (int i = 0; i < M; i++) {
        int num = sc.nextInt();
        int min = sc.nextInt();
        int count = 0;
        for (int j = 0; j < num; j++) {
          String course = sc.next();
          if (courses.contains(course))
            count++;
        }
        if (count < min)
          good = false;
      }

      if (good) {
        out.println("yes");
      } else {
        out.println("no");
      }
    }

    out.close();
    sc.close();
  }

}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.