import java.util.Scanner;
import java.io.*;

/*
 * OpinionDriver - Program to determine the number of opinions on the growth 
 *                 of the lemming population
 * Author:         William Kreahling
 * Date:           Feb, 2006
 */
class OpinionDriver
{
   public static void main(String[] args) throws Exception
   {
      final int PEOPLE;    // Number of people studying lemmings

      // Scanner for reading in from a file
      Scanner fileIn = new Scanner (new File("opinion2.dat")); 
      
      // There should only be ints in the file
      if (!fileIn.hasNextInt())
      {
         System.out.println("Invalid data in opinion.dat");
         System.exit(1);
      }
      // thie first line of the file is the number of students
      PEOPLE = fileIn.nextInt();

      int [] students = new int[PEOPLE]; // array of students
      int index;                         // index into the array
      int i = 0;                         // counter

      // initalize array
      for (i = 0; i < PEOPLE; i++)
         students[i] = 0;
      
      // Get students number (which we will use as an index)
      while (fileIn.hasNextInt())
      {
         // start counting students at 1, so subtract 1 
         index = fileIn.nextInt() - 1;

         // get number of lemmings sited
         if (!fileIn.hasNextInt())
         {
            System.out.println("Invalid data in opinion.dat");
            System.exit(1);
         }
         students[index] += fileIn.nextInt();
      }
      
      int currentNum;      // Lemmings current student saw
      int opinion = 0;     // number of opinions

      // Check all the students
      for(i = 0; i < PEOPLE; i++)
      {
         // lemmings seen by current student
         currentNum = students[i];

         // negative one means this opinion was already counted
         if (currentNum != -1)
         {
            System.out.print("Student(s) " + (i + 1));
            opinion++;
            
            // see how many other students agree with current one
            for (int j = i; j < PEOPLE; j++)
            {
               if (students[j] == currentNum && i != j)
               {
                  // if this student agreed with the current student
                  // empty the array so we don't try to count him again
                  // as having a separate opinion later on
                  students[j] = -1;
                  System.out.print(", " + (j + 1));
               }
                  
            }
            System.out.println(" share the same opinion");
         }
      }
      System.out.println("There are a total of " + opinion + " opinions");
   }
}
