Advertisement
Guest User

Untitled

a guest
Jun 1st, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     Scanner in;
  7.     PrintWriter out;
  8.  
  9.     private void run() {
  10.         in = new Scanner(System.in);
  11.         out = new PrintWriter(System.out);
  12.         try {
  13.             solve();
  14.         } finally {
  15.             out.close();
  16.         }
  17.     }
  18.  
  19.     class Prize {
  20.         People buyer;
  21.  
  22.         Prize(People buyer) {
  23.             this.buyer = buyer;
  24.         }
  25.     }
  26.  
  27.     class People {
  28.         Queue<Prize> queue = new ArrayDeque<>();
  29.         int id;
  30.  
  31.         People(int id) {
  32.             this.id = id;
  33.         }
  34.  
  35.         boolean hasPrize() {
  36.             return !queue.isEmpty();
  37.         }
  38.     }
  39.  
  40.     private void solve() {
  41.         int nTests = in.nextInt();
  42.         for (int t = 1; t <= nTests; t++) {
  43.             int nPeoples = in.nextInt();
  44.             People people[] = new People[nPeoples];
  45.             for (int i = 0; i < people.length; i++) {
  46.                 people[i] = new People(i + 1);
  47.             }
  48.             int nMoves = in.nextInt();
  49.             for (int move = 1; move <= nMoves; move++) {
  50.                 People from = people[in.nextInt() - 1];
  51.                 People to = people[in.nextInt() - 1];
  52.                 Prize gift;
  53.                 if (from.hasPrize()) {
  54.                     gift = from.queue.poll();
  55.                 } else {
  56.                     gift = new Prize(from);
  57.                 }
  58.                 if (gift.buyer == to) {
  59.                     out.println("YES");
  60.                 } else {
  61.                     out.println("NO");
  62.                 }
  63.                 to.queue.add(gift);
  64.             }
  65.         }
  66.     }
  67.  
  68.     public static void main(String[] args) {
  69.         new Main().run();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement