eolymp
bolt
Try our new interface for solving problems
Məsələlər

Exclusive Access 2

Exclusive Access 2

Having studied mutual exclusion protocols in the previous year's competition you are now facing a more challenging problem. You have a big enterprise system with a number concurrently running processes. The system has several resources -- databases, message queues, etc. Each concurrent process works with two resources at a time. For example, one process might copy a job from a particular database into the message queue, the other process might take a job from the message queue, perform the job, and then put the result into some other message queue, etc. All resources are protected from concurrent access by mutual exclusion protocols also known as \textit{locks}. For example, to access a particular database process acquires the lock for this database, then performs its work, then releases the lock. No two processes can hold the same lock at the same time (that is the property of mutual exclusion). Thus, the process that tries to acquire a lock \textit{waits }if that lock is taken by some other process. The main loop of the process that works with resources \textit{P }and \textit{Q }looks like this: loop forever DoSomeNonCriticalWork() P.lock() Q.lock() WorkWithResourcesPandQ() Q.unlock() P.unlock() end loop The order in which locks for resources P and Q are taken is important. Consider a case where process \textit{c }had acquired lock \textit{P }with P.lock() and is waiting for lock \textit{Q }in Q.lock(). It means that lock \textit{Q }is taken by some other process \textit{d}. If the process \textit{d }is working (not waiting), then we say that there is a \textit{wait chain }of length 1. If \textit{d }had acquired lock \textit{Q }and is waiting for another lock \textit{R}, which is acquired by a working process \textit{e}, then we say that there is a \textit{wait chain }of length 2, etc. If any process in this wait chain waits for lock \textit{P }that is already taken by process \textit{c}, then we say that the wait chain has infinite length and the system \textit{deadlocks}. For this problem, we are interested only in alternating wait chains where processes hold their first locks and wait for the second ones. Formally: \textit{Alternating wait chain }of length \textit{n }(\textit{n >= }0) is an alternating sequence of resources \textit{R_i }(0 <=\textit{ i }<=\textit{ n}+1) and distinct processes \textit{c_i }(0 <=\textit{ i }<=\textit{ n}): \textit{R}_0 \textit{c}_0 \textit{R}_1 \textit{c}_1 …\textit{ R_n c_n R_n}_\{+1\}, where process \textit{c_i }acquires locks for resources \textit{R_i }and \textit{R_i}_\{+1 in this order. Alternating wait chain is a deadlock when \}\textit{R}_0 = \textit{R_n}_\{+1\}. You are given a set of resources each process works with. Your task is to decide the order in which each process has to acquire its resource locks, so that the system never deadlocks and the maximum length of any possible alternating wait chain is minimized. \InputFile The first line of the input file contains a single integer \textit{n }(1 <=\textit{ n }<=\textit{ }100) -- the number of processes. The following \textit{n }lines describe resources that each process needs. Each resource is designated with an uppercase English letter from L to Z, so there are at most 15 resources. Each line describing process contains two different resources separated by a space. \OutputFile On first line of the output file write a singe integer number \textit{m --} the minimally possible length of the maximal alternating wait chain. Then write \textit{n }lines -- one line per process. On each line write two resources in the order they should be taken by the corresponding process to ensure this minimal length of the maximal alternating wait chain. Separate resources on a line by a space. If there are multiple satisfying orderings, then write any of them. The order of the processes in the output should correspond to their order in the input.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
2
P Q
R S
Çıxış verilənləri #1
0
P Q
R S
Müəllif Roman Elizarov