import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Problem
{
    // Instance data
    int n_of_events;
    int n_of_rooms;
    int n_of_students;
    int n_of_periods;
 
    ArrayList[] list_events_x_student;
    ArrayList[] list_unavailable_x_event;
    ArrayList[] list_suitable_x_event;
    int precedences[][];
    
    /**
     * Load data from a file
     * 
     * @param filename
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void loadFromFile(String filename) throws FileNotFoundException, IOException{

        BufferedReader reader = new BufferedReader(new FileReader(filename));
        
        for(String s = reader.readLine();s!=null;s=reader.readLine()){
	    // c comment
            if(s.matches("c.*")){
                continue;
            }
	    // p problem dimensions
            if(s.matches("p.*")){
		String[] tokens = s.split(" ");
		if (tokens.length != 4 && tokens.length != 6){
		    throw(new IllegalArgumentException("File has invalid format."));
		}
		n_of_events  = Integer.parseInt(tokens[1].trim());
		n_of_rooms  = Integer.parseInt(tokens[2].trim());
		n_of_students  = Integer.parseInt(tokens[3].trim());
		if (tokens.length == 6)
		    n_of_periods = Integer.parseInt(tokens[4].trim())*Integer.parseInt(tokens[5].trim());
		else	
		    n_of_periods = 45;
		
		list_events_x_student = new ArrayList[n_of_students];
		for (int i=0; i<n_of_students; i++)
		  list_events_x_student[i] = new ArrayList();
		list_unavailable_x_event = new ArrayList[n_of_events];
		list_suitable_x_event = new ArrayList[n_of_events];
		precedences = new int[n_of_events][n_of_events];		
		for (int e=0; e<n_of_events; e++)
		    {
		    list_unavailable_x_event[e] = new ArrayList();
		    list_suitable_x_event[e] = new ArrayList();
		    for (int e1=0; e1<n_of_events; e1++)
			precedences[e][e1]=0;
		    }
		continue;
            }
	    // e events attended by a student 
            if(s.matches("e.*")){
		s = s.trim();
		String[] tokens = s.split(" ");
		//if (tokens.length != 3){
		//    throw(new IllegalArgumentException("File has invalid format."));
		//}
		int size = tokens.length;
		int student = Integer.parseInt(tokens[1]);
		for (int i=2;i<size;i++)
		    {
			int event = Integer.parseInt(tokens[i]);
			list_events_x_student[student].add(event);
		    }
                continue;
            }
	    // u event unavailabilities
            if(s.matches("u.*")){
		s = s.trim();
		String[] tokens = s.split(" ");
		int size = tokens.length;
		int event = Integer.parseInt(tokens[1]);
		for (int i=2;i<size;i++)
		    {
			int period = Integer.parseInt(tokens[i]);
			list_unavailable_x_event[event].add(period);
		    }
                continue;
            }
	    // a precedences
            if(s.matches("a.*")){
		s = s.trim();
		String[] tokens = s.split(" ");
		if (tokens.length != 3){
		    throw(new IllegalArgumentException("File has invalid format."));
		}
		int event1 = Integer.parseInt(tokens[1]);
		int event2 = Integer.parseInt(tokens[2]);
		precedences[event1][event2]=1;
		precedences[event2][event1]=-1;
                continue;
            }
	    // r rooms suitable for an event
            if(s.matches("r.*")){
		s = s.trim();
		String[] tokens = s.split(" ");
		int size = tokens.length;
		int event = Integer.parseInt(tokens[1]);
		for (int i=2;i<size;i++)
		    {
			int room = Integer.parseInt(tokens[i]);
			list_suitable_x_event[event].add(room);
		    }
                continue;
            }
            throw(new IllegalArgumentException("File has invalid format."));
        }
        
        for(String s = reader.readLine();s!=null;s=reader.readLine()){
            if (s.equals("EOF")){
                break;
            }
        }
// 	System.out.println(n_of_events);
// 	System.out.println(n_of_rooms);
// 	System.out.println(n_of_students);
//	System.out.println(n_of_periods);
 
// 	System.out.println(list_events_x_student[0]);
// 	System.out.println(list_events_x_student.length);
// 	System.out.println(list_unavailable_x_event[0]);
// 	System.out.println(list_unavailable_x_event.length);
// 	System.out.println(list_suitable_x_event[0]);
// 	System.out.println(list_suitable_x_event.length);
// 	System.out.println(precedences[0][0]);
    }

}
	
