# Read input data
import sys
import threading
def main():
import sys
import heapq
sys.setrecursionlimit(1 << 25)
input_lines = sys.stdin.read().splitlines()
idx = 0
while idx < len(input_lines) and input_lines[idx].strip() == '':
idx +=1
assert idx < len(input_lines)
assert input_lines[idx].strip() == 'GRAPH', 'First section must be GRAPH'
idx +=1
graph = {}
while idx < len(input_lines) and input_lines[idx].strip() != 'PROGRAM':
line = input_lines[idx].strip()
if line == '':
idx +=1
continue
u, v = map(int, line.strip().split())
if u not in graph:
graph[u] = []
graph[u].append(v)
idx +=1
assert idx < len(input_lines) and input_lines[idx].strip() == 'PROGRAM', 'Second section must be PROGRAM'
idx +=1
step_programs = {} # step_id: list of programs
program_steps = {} # program_name: set of steps that use it
while idx < len(input_lines) and input_lines[idx].strip() != 'MACHINE':
line = input_lines[idx].strip()
if line == '':
idx +=1
continue
tokens = line.strip().split()
if len(tokens) >= 2:
step_id = int(tokens[0])
programs = tokens[1:]
step_programs[step_id] = programs
for p in programs:
if p not in program_steps:
program_steps[p] = set()
program_steps[p].add(step_id)
idx +=1
assert idx < len(input_lines) and input_lines[idx].strip() == 'MACHINE', 'Third section must be MACHINE'
idx +=1
program_machines = {} # program_name : machine_count
while idx < len(input_lines):
line = input_lines[idx].strip()
if line == '':
idx +=1
continue
tokens = line.strip().split()
if len(tokens) == 2:
p_name, num = tokens
num = int(num)
program_machines[p_name] = num
idx +=1
# Now process the data
# First, build the full list of steps and dependencies
all_steps = set(step_programs.keys())
for u in graph.keys():
all_steps.add(u)
all_steps.update(graph[u])
N = max(all_steps) # Max step ID
# Perform topological sort and compute EETs
visited = {}
temp_mark = set()
EET = {}
def dfs(u):
if u in temp_mark:
raise Exception('Graph has cycles')
if u in visited:
return
temp_mark.add(u)
for v in graph.get(u, []):
dfs(v)
temp_mark.remove(u)
visited[u] = True
for u in all_steps:
if u not in visited:
dfs(u)
# Now compute EETs
EET = {}
indegree = {}
for u in all_steps:
indegree[u] = 0
for u in graph:
for v in graph[u]:
indegree[v] += 1
from collections import deque
queue = deque()
for u in all_steps:
if indegree[u] == 0:
queue.append(u)
EET[u] = 0
while queue:
u = queue.popleft()
for v in graph.get(u, []):
EET_v = EET[u] + 1
if v not in EET or EET_v > EET[v]:
EET[v] = EET_v
indegree[v] -=1
if indegree[v] == 0:
queue.append(v)
# For steps not in graph (no dependencies), ensure they have EET
for u in all_steps:
if u not in EET:
EET[u] = 0 # No dependencies
# Now, for each program, compute earliest and latest usage times
program_intervals = {} # program_name: [start_time, end_time]
for p in program_steps:
steps_using_p = program_steps[p]
earliest = min(EET[s] for s in steps_using_p)
latest = max(EET[s] for s in steps_using_p)
program_intervals[p] = [earliest, latest]
# Create events: time, event_type (start=1, end=0), program_name
events = []
for p in program_intervals:
s, e = program_intervals[p]
events.append((s, 1, p)) # Start event
events.append((e + 1e-6, 0, p)) # End event; slightly increment time to ensure end after start at same time
# Sort events
events.sort()
total_machines = 0
current_programs = set()
time_points = []
time_machines = []
for time, event_type, p in events:
if event_type == 1:
# Start event
current_programs.add(p)
total = sum(program_machines[cp] for cp in current_programs)
time_points.append(time)
time_machines.append(total)
else:
# End event
current_programs.remove(p)
total = sum(program_machines[cp] for cp in current_programs)
time_points.append(time)
time_machines.append(total)
min_machines = max(time_machines)
print(int(min_machines))
# Now assign machines to programs
# We need to process steps in order, and programs in lex order
# For each program, assign machines starting from lowest available machine IDs
# Ensure that overlapping programs are not assigned overlapping machines
# We need to know for each program the set of overlapping programs
# Let's build for each program the set of times when it's active
# Process the steps in order
assigned_programs = {}
machine_used = set()
# We need to keep track of which machines are used by overlapping programs
# For each program, keep track of assigned machines
program_assigned_machines = {}
# Build a list of programs in the order they will be processed
steps_list = sorted(all_steps)
program_order = []
for step_id in steps_list:
programs_in_step = step_programs.get(step_id, [])
programs_in_step.sort()
for p in programs_in_step:
key = (EET[step_id], step_id, p)
program_order.append((key, step_id, p))
# Now sort program_order
program_order.sort()
# For each program in order, assign machines
for key, step_id, p in program_order:
if p in program_assigned_machines:
continue # Already assigned
# Need to assign machines for program p
s_time, e_time = program_intervals[p]
# Find overlapping programs
overlapping_programs = []
for op in program_assigned_machines:
os_time, oe_time = program_intervals[op]
if not (e_time < os_time or oe_time < s_time):
# Overlaps
overlapping_programs.append(op)
# Collect used machines
used_machines = set()
for op in overlapping_programs:
used_machines.update(program_assigned_machines[op])
# Now find the smallest block of consecutive machine IDs not used
required_machines = program_machines[p]
start_machine = 0
while True:
assigned = set(range(start_machine, start_machine + required_machines))
if assigned.isdisjoint(used_machines):
# Assign these machines
program_assigned_machines[p] = list(range(start_machine, start_machine + required_machines))
break
else:
start_machine +=1
# No need to update machine_used since we check overlapping programs
# Now for each step, output the assignments
for step_id in steps_list:
programs_in_step = step_programs.get(step_id, [])
if not programs_in_step:
continue # No programs in this step
output_line = [str(step_id)]
programs_in_step.sort()
for p in programs_in_step:
start_machine = min(program_assigned_machines[p])
output_line.extend([p, str(start_machine)])
print(' '.join(output_line))
threading.Thread(target=main).start()