POLISH OLYMPIAD IN INFORMATICS
The 7th Balti Olympiad in Informati s BOI 2001 Sopot, Poland
Tasks and Solutions Edited...
35 downloads
761 Views
418KB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
POLISH OLYMPIAD IN INFORMATICS
The 7th Balti Olympiad in Informati s BOI 2001 Sopot, Poland
Tasks and Solutions Edited by Mar in Kubi a
WARSAW, 2001
Authors: Mar in Kubi a Ville Leppänen Adam Malinowski Oleg Mürk Krzysztof Onak Martins Opmanis Mindaugas Plukas Wolfgang Pohl Ahto Truu Piotr Sankowski Mar in Sawi ki Mar in Stefaniak Tomasz Wale« Paweª Wol
Proofreader: Weronika Walat Volume editor: Mar in Kubi a Typesetting: Krzysztof Onak Tomasz Wale« Sponsored by Polish Ministry of National Edu ation.
ISBN 8390630184
Contents Prefa e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Ex ursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
Box of Mirrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Cra k the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Postman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Knights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Mars Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Teleports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3
Mar in Kubi a
Prefa e
Balti Olympiad in Informati s (BOI) gathers the best teen-age programmers from ountries surrounding the Balti Sea. The 7-th BOI was held in Sopot, Poland, June 1621, 2001. Eight ountries took part in the
ompetition: Denmark, Estonia, Finland, Germany, Latvia, Lithuania, Poland and Sweden. All of the ountries, ex ept Denmark and Poland, were represented by 6 ontestants. Denmark was represented by one and Poland by 12 ontestants. BOI'2001 was organized by Polish Olympiad in Informati s together with Prokom Software S.A., Combidata Poland Sp. z.o.o. and the ity of Sopot. More information about this ompetition, an be found at www.ii.uni.wro .pl/boi/. The ontest onsisted of three sessions: a trial session and two ompetition sessions. During the trial session
ontestants had an o
asion to be ome familiar with the software environment and to solve a warming up task `Ex ursion'. During ea h of the ompetition sessions they had to solve three tasks within ve hours of time: `Box of Mirrors', `Cra k the Code' and `Postman' during the rst session and `Knights', `Mars Maps' and `Teleports' during the se ond one. Four ontestants were awarded gold medals, eight ontestants were awarded silver medals and twelve ontestants were awarded bronze medals. The results are as follows:
Gold medalists: Martin Pettai Mi haª Adamaszek
Paweª Parys Krzysztof Klu zek
Poland Poland
Germany Poland Poland Poland
Ivars Atteka Uldis Barbans Piotr Stan zyk Tobias Polley
Latvia Latvia Poland Germany
Finland Germany Poland Sweden Lithuania Denmark
Hendrik Nigul Mar in Mi halski Girts Folkmanis Benjamin Dittes Kristo Tammeoja Fredrik Jansson
Estonia Poland Latvia Germany Estonia Finland
Silver medalists: Daniel Jasper Tomasz Malesi«ski Karol Cwalina Arkadiusz Pawlik
Estonia Poland
Bronze medalists: Teemu Murtola Mi hael Siepmann Marek Zylak Daniel Andersson Viktor Medvedev Bjarke Roune
This booklet presents tasks from BOI'2001 together with the dis ussion of their solutions. Some more materials, in luding test data used during the evaluation of solutions, an be found at www.ii.uni.wro .pl/boi/. It was prepared for the ontestants of various programming ontests to help them in their exer ises. Mar in Kubi a
Task by
Solution des ription by
Mindaugas Plukas
Mar in Stefaniak
Ex ursion A group of travelers has an opportunity to visit several ities. Ea h traveler states two wishes on what ity he/she does want or does not want to visit. One wish expresses a will to visit or not to visit exa tly one ity. It is allowed that both wishes of one traveler are the same or that they are opposite|i.e. I want to visit ity A, and I do not want to visit ity A. Task
Your task is to write a program that:
reads the travelers' wishes from the input le ex .in, determines whether it is possible to form su h a list of ities to be visited (the list an be empty), that at least one wish of every traveler is satis ed, writes the list of ities to be visited to the output le ex .out.
If there are several possible solutions, your program should output anyone of them. Input
The rst line of the input le ex .in ontains two positive integers n and m (1 n 20 000 , 1 m 8 000 ); n is the number of travelers, and m is the number of ities. The travelers are numbered form 1 to n, and the ities are numbered from 1 to m. Ea h of the following n lines ontains two nonzero integers, separated by single spa e. The i + 1 -th line
ontains numbers wi and wi0 representing wishes of the i-th traveler, m wi ; wi0 m, wi ; wi0 6= 0 . A positive number means that the traveler wishes to visit that ity, and a negative number means that the traveler does not wish to visit the
ity spe i ed by the absolute value of the number. Output
Your program should write one nonnegative integer l, the number of ities to be visited in the rst line of the output le ex .out. In the se ond line of the le there should be written exa tly l positive integers in the as ending order, representing
ities to be visited. In ase it is not possible to form su h a list of ities, your program should write in the rst and only line the word NO. Example
For the input le ex .in: 3 1 2 3
4 -2 4 1
the orre t result is the output le ex .out: 4 1 2 3 4
Solution
A bit of logi In the task `Ex ursion', the goal is to nd su h an ex ursion routea subset of a given set of ities, that satises every traveler. There is something spe ial about the travelers' wishes, namely, every wish is an alternative of exa tly two simple onditions, like `to visit the ity' or `not to visit the ity'. We an easily express these wishes in the language of propositional logi . For example, the input le:
3 1 2 3
4 -2 4 1
Ex ursion
8
means that:
there are 3 travelers and 4 ities, the rst traveler wants to visit the 1-st ity or not to visit the 2-nd ity, the se ond traveler wants to visit the 2-nd ity or the 4-th ity, the third traveler wants to visit the 3-rd ity or the 1-st ity.
Let us denote by ai the senten e `the ex ursion is to visit the ity number i'. These are alled variables, sin e their logi al values (whether they are true or false) are not xed. In our example, there are four variables a1 ; a2 ; a3 ; a4 . Now, the travelers' onditions an be expressed by the following formulae: 8
< a1 _ :a2 : aa23 __ aa41
Sin e all the travelers are going to the same ex ursion, all of these onditions have to be satised together:
(a _ :a ) ^ (a _ a ) ^ (a _ a ) 1
2
2
4
3
1
This formula is equivalent to the ex ursion problem from the example le. To solve the problem one should satisfy this formula (assign logi al valuestrue, falseto the variables, so that the formula is true), or show that there is no su h assignment (implying that the ex ursion is impossible). In general, the input le an be rearranged and transformed into a logi al formula, whose satisfa tion is equivalent to solving the ex ursion problem. The problem of satisfying a given logi al formula is well known to be NP- omplete1 and hardly ever is an NP- omplete problem believed to be solvable in a polynomial time. Fortunately, the formula obtained in this task is of a very spe ial form:
(l _ l ) ^ (l _ l ) ^ : : : ^ (l _ l ) 1 1
2 1
1 2
2 2
1
m
2
m
where lj1 , lj2 are alled literals and ea h of them stands for some variable ai or negated variable :ai . The literals lj1 and lj2 orrespond to the onditions of the j -th traveler. The formula is a onjun tion of alternatives of exa tly two literals. Therefore it is said to be in the se ond onjun tive normal form, (i.e. 2-CNF). We are parti ularly lu ky that the tourists stated at most two onditions. If they were allowed to state three of them, the alternatives in the formula would onsist of three literals, hen e the formula would not be in 2-CNF, but in 3-CNF form, and it has been proven that satisfying 3-CNF formula is another NP- omplete problem. That proof and more information on NP- ompleteness an be found in [1℄. However, the ex ursion problem is equivalent to satisfying a formula of 2-CNF form, and we shall show that this problem an be solved in a polynomial, linear time.
Ex ursion and graphs We shall onsider an undire ted graph
G = (V; E ) with verti es orresponding to all possible literals:
V
= fa ; :a ; a ; :a ; : : : ; a ; :a g 1
1
2
2
n
n
and edges onne ting pairs of literals whi h appear in alternatives of the formula:
E = f(l1; l2 ) : i = 1; 2; : : : ; mg i
The graph
G
i
a1
a2
a3
a4
~a1
~a2
~a3
~a4
for the example input.
1 We onsider here propositional logi (without quantiers and predi ates, whi h would make things even worse).
Ex ursion Our goal is to sele t a subset W V ontaining verti es orresponding to these literals whi h are true when the formula is satised. For that, the following holds and must hold:
() either a 2 W , or :a 2 W (ex lusively), for i = 1; 2; : : : ; n, (|) for any edge (u; v) 2 E , u 2 W or v 2 W (i.e. u _ v), We say verti es a , and :a are opposite, be ause one and only one of them must belong to the set W . Suppose (u; v ) 2 E , or u _ v . By laws of logi we obtain useful impli ations: u _ v () :u ) v () :v ) u i
i
i
i
We onstru t a dire ted graph
G0 = (V; E 0 ), whose edges represent these impli ations: E 0 = f(:u; v) : (u; v) 2 E _ (v; u) 2 E g
We shall all this the inferen e graph, be ause it an be used to nd whi h verti es one has to hoose to the set W , provided that some given vertex has already been hosen. The inferen e graph has n verti es and m edges, where n is the number of ities and m denotes the number of travelers (this diers from the task statement, but we shall sti k here to the usual onvention).
2
2
a1
a2
a3
a4
~a1
~a2
~a3
~a4
The example of inferen e graph
0
G
. Edges of the original graph are dashed.
In our example, we an now learly see, that if one adds the vertex :a4 to the set W , one must also in lude verti es a2 , and onsequently a1 , be ause these verti es an be rea hed from :a4 through the edges of the inferen e graph. Every W V whi h is a orre t solution of the problem must adhere to the inferen e graph, in su h a way that:
(})(w 2 W ^ (w; v) 2 E 0 ) ) v 2 W: that satises (}) and (), forms a orre t solution to the ex ursion problem, sin e
At the same time, any W } () | , whi h is an obvious onsequen e of the way in whi h the graph G0 was onstru ted. Therefore it is enough to sear h for a W V adhering to the inferen e graph, and not ontradi ting the ex lusiveness
ondition . Let us denote
( )
( ) ( )
(w) = fv : w ! vg;
Indu ed
where ! means that there is a path from equivalent to } :
( )
( )
w
to
w2W
v
in the graph
G0 .
The following property holds, sin e it is
) Indu ed (w) W:
If a set Indu ed w ontains two opposite verti es (whi h is not allowed in a solution), then we say that the vertex w is troublesome. Another graph whi h is useful in solving this problem is the oni t graph. For ea h u; v 2 E () u _ v , by laws of logi
( )
hen e the verti es :u and
u_v
() :(:u ^ :v);
:v are in oni t, and annot be both present in the solution set W .
9
10
Ex ursion
a1
a2
a3
a4
~a1
~a2
~a3
~a4
The oni t graph for the example input. The edges of the graph
G
are dashed.
Simple algorithm The following algorithm solves the problem: 1:
= ;;
W
while begin
2: 3:
W
< n do
be a ity, su h that ; : 62 W ; : are troublesome then solution does not exist, STOP
let
4: 5:
if both and
6:
else begin
7: 8:
let
9:
W
10: 11:
end
12: 13:
end
v
be a non-troublesome vertex
:= W [ Indu ed (v);
or
: ;
The algorithm hooses a vertex a
epting or reje ting a ity, and then expands the set W by indu ed verti es. Noti e that ities that are not mentioned in any way in the set of indu ed verti es do not oni t with the ities that are. This is the very key to the solution, as it allows to solve the problem without sear hing re ursively the whole spa e of possible solutions.
Independen e property
=
()
Let U Indu ed v and let M be su h a set of verti es that neither they nor the verti es opposite to them are in the set U . If the vertex v is not a troublesome one, then there is no edge in the oni t graph between U and M . This fa t might seem quite hard to see at rst glan e, but it is easy to show. Should a oni t happen between m 2 M and u 2 U , then u ! :m, so :m 2 Indu ed u Indu ed v U . But :m 62 U by the denition of M . As the result of the independen e property, the algorithm, in ea h turn of the loop, sear hes for verti es in a similar way, as it does in the beginning. Furthermore, any algorithm, whi h does not a
ept troublesome verti es and would not a
ept a pair of opposite or oni ting verti es, eventually nds a orre t solution.
()
()=
Strongly onne ted omponents The algorithm presented in se tion `Simple algorithm' is not the fastest one. The time omplexity of he king if a vertex is troublesome may take time proportional to the size of the graph, so the overall time omplexity is O n n m . We shall show a more e ient algorithm, whi h solves the problem by al ulating strongly
onne ted omponents of the inferen e graph. Some of the readers may be familiar with the on ept of strongly onne ted omponents in a dire ted graph. We say that two verti es u; v belong to the same strongly onne ted omponent, when there is a path from u to v as well as from v to u. Certainly, being in the same strongly onne ted omponent is an equivalen e relation, hen e it divides the set of verti es into disjoint groups. These groups are alled, obviously, strongly onne ted
omponents. Strongly onne ted omponents of an inferen e graph have a useful property: for any omponent C V , either C W , or C \ W ;. In other words, we annot hoose a omponent partially; we an only take
( ( + ))
=
Ex ursion all verti es from that omponent, or none of them. Therefore we shall onsider the graph of omponents G V ; E , whose verti es are strongly onne ted omponents of the graph G0 , and edges are inherited from the graph in a natural way. There is an algorithm al ulating strongly onne ted omponents in a graph in a linear time in term of the size of the graph, O n m . The algorithm is quite simple, but tri ky. One an nd detailed explanations in e.g. [1℄.
=(
)
( + )
An example of strongly onne ted omponents in a graph.
Let us suppose for a while that there is a y le in the graph of omponents, then we ould easily see that all verti es in that y le should belong to the same omponentan obvious ontradi tion. Hen e, the graph of
omponents is a dire ted a y li graph (DAG). We shall sort topologi ally the DAG of omponents, so that we ould browse its verti es in a not-des ending order, a
ording to the graph of omponents. This an also be done in a linear time. We say that we a
ept a omponent, when (while performing the algorithm) the omponent is hosen and in luded to the set of true literals W . We also say that we reje t a omponent, if we de ide not to hoose the
omponent anyway (perhaps due to some ontradi tions). Note that reje ting a omponent entails reje ting all its as endants in DAG G , and similarly a
epting a
omponent leads to a
epting all its des endants. In fa t, the graph G is the graph of indu tive reasoning, whereas the reversed graph GT an be viewed as the graph of dedu tive reasoning; these metaphors might explain the name `inferen e graph'.
Algorithm The following are the main steps of the algorithm solving the problem: 1. read the input le and generate the inferen e graph
G0 ,
2. nd strongly onne ted omponents and the omponents graph
G
,
3. if there are two opposite verti es in a omponent, then reje t this omponent and all its as endants, 4. sort topologi ally the omponents, and pro ess them in as ending order: (a) if the urrent omponent has not been reje ted, then a
ept it , (b) for ea h vertex in the a
epted omponent reje t the omponent ontaining the opposite vertex (and
onsequently all its as endants), 5. if exa tly exist.
n
verti es have been a
epted, then they form a orre t solution; otherwise solution does not
Note that be ause of the topologi al ordering of omponents, ea h time we a
ept a omponent all of its des endants have already been a
epted (had one been reje ted before, then the urrent omponent would have been reje ted). We shall show that this algorithm really solves the problem. It is easy to see that if half of the verti es are a
epted, then this set of verti es is a orre t solution. Indeed, it does not ontain any oni ts, be ause of reje ting omponents in steps 3 and 4.b, and is onsistent with the inferen e graph, as we have pointed out. What we have to show then, is that if there exists a solution, then the algorithm will nd one. Noti e that when a vertex is a
epted the opposite one is reje ted. As it was explained in se tion `Independen e property', a
epting a vertex is harmless provided that it is not troublesome. But all the troublesome
11
12
Ex ursion
verti es are reje ted in the step 3, and sin e we are pro essing omponents in as ending order, we are avoiding
oni ts. Therefore, from the independen e property omes the orre tness of the solution algorithm. Other solutions
Naive solution One ould solve this problem by rude ba ktra king. In this algorithm, we onsider a ity i, whi h is yet neither a
epted nor reje ted. We try to a
ept it and using the inferen e graph G0 we an nd its des endant verti es, whi h are also a
epted. Meanwhile, we he k if a ontradi tion has o
urred. If so, we try the se ond option, namely reje ting the ity. In ase of a su
essful try, we onsider another ity, and so on; if both de isions failed, we ba ktra k. The time omplexity of this algorithm is exponential, so it is a very slow solution. Tests
The tests have been generated randomly for various number of ities and travelers, in reasing gradually in order to distinguish dierent solutions. The tests are grouped in order to dete t programs answering always `NO'. (A program ould s ore points only for solving all tests in a group). The test group number 8 was espe ially designed to dis ern non-linear solutions O n m m from the linear ones.
(( + ) )
No 1 2 3 4a 4b 5a 5b 6a 6b 7 8a 8b 8
No of travelers 27 107 1010 48 106 257 3022 829 1006 20000 15998 15998 16000
No of ities 10 33 100 100 100 333 400 1000 1000 8000 8000 8000 8000
Remarks
answer NO answer NO answer NO
Task by
Solution des ription by
Martins Opmanis
Piotr Sankowski
Box of Mirrors Mathemati ian Andris likes dierent puzzles and one of his favorites is a overed box of mirrors. If we look at a horizontal
ross-se tion of su h a box, we an see that its bottom part ontains n m square ells (n rows, and m olumns). In ea h
ell there an be pla ed a mirror whi h is oriented diagonally from lower left orner to upper right orner. Both sides of the mirror re e t light. At the box edges lo ated at both ends of ell rows and olumns there are gaps through whi h you
an light a beam into the box or a beam an ome out of the box. Through ea h gap you an light a beam in only one dire tion|perpendi ular to the edge ontaining the gap. Therefore, a beam re e ted from a mirror hanges its dire tion by 90 deg. When the beam goes through empty ells, its dire tion doesn't hange. Gaps are numbered onse utively from 1 to 2 ( n + m), around the box, ounter- lo kwise, starting from the gap on the left side of the upper left ell and going downwards. Sin e the arrangement of mirrors in the box is not visible, the only way to determine it is by lighting beams into some gaps and wat hing where the light omes out. Task
Write program that:
reads the size of the box and gaps des ribing beams oming in and out of the box from the input le box.in, determines in whi h ells there are mirrors and whi h ells are empty, writes the result to the output le box.out.
If there are several possible solutions, your program should output anyone of them. Input
First line of input le box.in ontains two positive integers: n (the number of ell rows, 1 n 100 ) and m (the number of ell olumns, 1 m 100 ) separated by a single spa e. Ea h of the following 2 ( n + m) lines ontains one positive integer. The number in the i + 1 -th line denotes the number of the gap from whi h the light omes out if it is lightened into gap number i. Output
Your program should write to the output le box.out n lines, ea h of them ontaining m integers separated by single spa es. The j -th number in the i-th line should be 1, if there is a mirror in the ell in the i-th row and j -th olumn of the box, or it should be 0 if the ell is empty. Example
For the input le box.in: 2 3 9 7 10 8 6 5 2 4 1 3
the orre t result is the output le box.out: 0 1 0 0 1 1
14
Box of Mirrors 10
9
8
1
7
2
6 3
4
5
Solution
The solution of this problem is very easy, but as is often the ase, it's not so easy to nd. The problem an be solved by a greedy algorithm. A greedy algorithm might also be alled a `single-minded' algorithm, an algorithm that takes are of only one thing. It performs a single pro edure in a re ipe over and over again until su h a pro ess annot be ontinued anymore. In every step it enlarges the solution by the best element or just by an element that fullls some property. In most ases su h an algorithm doesn't produ e an optimal solution, but in some it does. Many su h ases are des ribed by a matroid theory. For more information on this topi see [1℄. The matroid theory provides a stri t way of proving the orre tness of a greedy algorithm. One only has to he k if every step satises some given onditions. The orre tness of the algorithm for this problem will be proved in a similar way. We will show that every step of the algorithm will not hinder the following steps. Every step will onsist of nding a tra k for one beam, and it will be done in su h a way that it won't make it impossible to nd tra ks for other beams. Our algorithm will su
essively tra k beams lighted into gaps with numbers ; ; :::; n m. At the beginning the box will not ontain any mirrors, they will be pla ed where needed while tra king the beams. They will also be pla ed in su h a way that the beam goes as mu h as possible upwards, in order to not `disturb' light beams going underneath. Let us onsider the following algorithm to tra k beams:
12
1. Let x and y be the urrent olumn and row of the tra ked beam. Let the gap through whi h the beam should leave. 2. If
x = x0
and
y = y0
+
x0 and y0 be the olumn and row of
then stop.
3. If the beam goes verti ally go to step 5.
=
=
4. If there is no mirror above the urrent position and x 6 x0 or y y0 , then go one ell to the right: x x . Otherwise pla e a mirror in ell x; y and go one ell upwards: y y . Go to step 2.
:= + 1
( )
:=
1 5. If there is a mirror in the urrent ell, then go right: x := x + 1. Otherwise go up: y := y 1. Go to step 2.
In step 4 of the algorithm we go right if we are in the row in whi h there is the hole through whi h the beam should leave, or we go up and pla e a mirror in the urrent ell if we are in the olumn in whi h there is the destination hole or there is some mirror above, so we go as mu h as possible upwards. The following pi tures show how the rst three beams are tra ked for a box with 2 olumns and 3 rows and light beams ! ; ! ; ! ; ! and ! .
1 10 2 8 3 9 4 6
5 7
Box of Mirrors
Example of how the algorithm works for a a box with 2 olumns, 3 rows and light beams 1 ! 10; 2 ! 8; 3 ! 9; 4 ! 6 and 5 ! 7.
Note that, when a mirror is pla ed the light beam is ree ted on the left side of the mirror (step 4 of the algorithm). Other steps ontain no requirements, in other words nothing may go wrong with them, they only tra k the beam. Consider the ondition in point 4. There are only two possible situations when the algorithm might produ e in orre t solutions, both are shown in the following pi ture. We tra k the beam and in a ertain step it is in the row or olumn of the hole through whi h it should leave, but the tra ked beam annot be lighted out through the proper hole (marked with A) be ause there is a mirror in its way.
In orre t situations. The beam should leave through gap A, but there is a mirror in its way.
Su h situations annot happen in our algorithm, be ause there must be already some other light beam ree ted form the left side of the mirror, and in the ase shown on the left it has already left through this hole, but in our algorithm beams leave only through orre t holes, and two beams annot be lighted out through the same hole. In the ase on the right, there already is some other beam going in this row, so it is impossible too. Contestants' Solutions
The results s ored by the solutions of this problem were mostly either near maximum or not more than 20% of the points. Contestants usually either ame to the des ribed solution and s ored all points or they tried to implement some ba ktra king algorithm whi h worked in an exponential time and passed mostly two smallest tests. Tests
In this problem there are no spe ial ases, and so there are no spe ial test ases. All tests for this problem are randomin a box of some size there were pla ed some mirrors at random positions. The main goal of the
15
16
Box of Mirrors
tests was to distinguish between optimal solutions and exponential ones, and to he k the orre tness of the solutions.
Task by
Solution des ription by
Oleg Mürk and Ahto Truu
Paweª Wol
Cra k the Code Cryptography is the s ien e and te hnology of oding messages so that only the intended re ipient an read them. Cryptanalysis, on the other hand, is the s ien e of breaking the odes. For this problem, assume you're a ryptanalyst hired to break open a series of en rypted messages aptured in a poli e raid on the headquarters of the lo al ma a. Your olleagues have already reverse engineered the en ryption program (see ra k.pas or ra k. for the ode), and the only thing left to do is to reverse the algorithm and guess the key used to en rypt the les. Along with the en rypted les, there are also some plaintext les that are believed to originate from the same sour e as the en rypted les and thus have a similar stru ture in terms of language, word usage, et . Task
Your task is to de rypt the messages given and to save them in the spe i ed les. You do not have to provide any program|just the de rypted messages. Input
You are given several data sets. They onsist of les ran.*, where n is the number identifying the data set. Ea h data set onsists of the les:
ra *.in, en rypted message,
ra *.txt, plaintext les of the same origin, as the en rypted message.
Output
For ea h en rypted message ra *.in, you should save the de rypted message in the le ra *.out. Solution
En ryption program In this se tion we are going to explain, how the en ryption program ra k.pas ( ra k. ) works. First we des ribe a simple kind of ipher, known as the Caesar's ode. The pro ess of en rypting the text with su h a
ode is following. Let us x the en ryption keyin the ase of this ipher it is a single integer number k . The en rypted text results from hanging every single letter in the input text to letter whi h stands in the English alphabet ( y li -formed) k positions further. For example, if we have hosen number , as k then `CIW, SJ GSYVWI' is the en rypted version of `YES, OF COURSE'. (Noti e, that su h hara ters as spa es and ommas are unae ted). If we apply this algorithm with the key k to the text whi h has been en rypted in the way des ribed above, it will give us ba k the original text. Therefore as the result of oding the text `CIW, SJ GSYVWI' with the key (whi h means, that every letter is hanged to the letter standing positions before it) we will obtain the primary text `YES, OF COURSE'. The en ryption algorithm whi h was implemented in the program ra k.pas ( ra k. ) is a bit more
ompli ated. Very briey we an say that it is a ombination of ten various Caesar's odes. More pre isely, this
ipher an be des ribed as follows. Let us omit for a while all the hara ters in our text whi h are not letters. We divide the input text into several pie es, ea h onsisting of ten letters, ex ept for the last one whi h might be shorter. Now we form these pie es into an array, su h that one pie e is represented by one row and they are set one under another. The following example illustrates this:
4
4
4
I S I R A
T P N E R
W R G S T
A I I T E
S N N A D
A G T S O
F M H H U
I O E E T
N R F S
E N O T
The split text is: `IT WAS A FINE SPRING MORNING IN THE FOREST AS HE STARTED OUT.'
18
Cra k the Code
In the previous en ryption algorithm the en ryption key was a single integer number. Here the key is a sequen e of ten integers k1 ; : : : ; k10 . En rypting a text with su h a key onsists of applying Caesar's ode to ea h olumn of the array separately. It means that the rst olumn, onsidered as a text, is being en rypted by the Caesar's
ode with key k1 , the se ond olumnwith key k2 , et . For instan e, oding the text from the example with the key ; ; ; ; ; ; ; ; ; gives the following array:
(
)
(1 2 3 4 5 6 7 8 9 10)
J T J S B
V R P G T
Z U J V W
E M M X I
X S S F I
G M Z Y U
M T O O B
Q W M M B
W A O B
O X Y D
The en rypted version is: `JV ZEX G MQWO TRUMSM TWAXJPJ MS ZOM OYSGVX FY OM BDBTWII UBB.' What remains now is to join all the rows of the modied array and put all the forgotten hara ters into appropriate pla es. What we obtain is an en rypted version of the input text. (Note that the method des ribed above an be explained in a dierent way: the 1st, 11th, 21st, ... letter o
urring in the text is en rypted by the Caesar's ode with key k1 ; the 2nd, 12th, 22nd, ... letter o
urring in the textwith the key k2 ; et . and at last 10th, 20th, 30th, ... letter o
urring in the textwith the key k10 . The hara ters whi h are not letters are unae tedlike in the Caesar's ode).
Methods of ra king the ode First let us onsider methods whi h enable one to de rypt a message whi h is known to have been en rypted by Caesar's ode. We will see later, that some of these methods an by easily applied to the ase of the ipher whi h is used in our task. The simplest method is to onsider all values from the interval ; as a value of the key. Considering other values is unne essary, be ause using the key k gives the same result as using the key of value equal to the remainder of the division of k by the total number of letters in the English alphabet. This remainder obviously belongs to the interval ; . In ase the text is written in some natural language and its length is more than a few hara ters, it is quite ertain that for exa tly one value of the key we obtain some text whi h does make sense. Be ause of the abovementioned small number of various keys, it is possible to verify `manually' whi h key is the right one. However this method annot be used ee tively in the ase of messages en rypted by the se ond kind of ipher. In that situation we should guess not one, but ten numbers. It doesn't seem that it is possible to guess every key separately using `manual' veri ation, be ause guessing the rst number of a ten-number key de iphers only every tenth letter of the text. The de oded part of the text surrounded by of the oded one ouldn't be easily seen to be de iphered orre tly (in ontrast with the situation, where a one-number key de iphers of letters in the text). Veri ation is easy only if we try to de ipher the whole message by hoosing all ten elements of the key. Nevertheless there are 10 ombinations to he k, therefore manual veri ation of all these ases is impossible. Let us then show other methods. We make an assumption that we have at our disposal some plaintext as well as a message whi h is believed to be en rypted by Caesar's ode and to have the same origin as the rst one. We wish our methods to nd the key (at this time automati ally) whi h de iphers the given message. These methods are based on the fa t that in a natural language the frequen y of o
urren e of ea h letter of the alphabet is dierent. For instan e in English the frequen ies are as follows:
[0 25℄
26 [0 25℄
90%
100%
26
Letter(s) E T A I, O, S ... J, K, Q, X, Z
Frequen y of o
urren e
11% 9% 8% 7%
... less than
1%
Hen e, if the plaintext is in English then E should be the most ommon. So it follows that if J is the most
ommon letter in the en rypted message, we may suppose that the key had been used (be ause J stands positions further than E). In this ase the key mod should be used for de rypting the message. This method does not always give the orre t solution. Let us onsider a situation, where some two letters o
ur with similar frequen ies (e.g. E and T in English). If the en rypted message is not too long it may happen that T appears more often than E. A message like that would be de iphered in orre tly. For example, if the
5 21(
26)
5
5
Cra k the Code en rypted message is `ZNOY OY G YZGZKSKTZ' then Z appears the most often and it would be de oded as E, giving the message `ESTD TD L DELEPXPYE', whi h ertainly doesn't make sense. In fa t Z should be de oded as T whi h would give the answer `THIS IS A STATEMENT'. Let us try to nd a better method of de iding whether a given key is the right one. It will be onvenient to introdu e the following denitions:
p
i
frequen y of o
urren e of i-th letter of alphabet in plaintext (i.e.
p1
q
i
frequen y of o
urren e of i-th letter of alphabet in en rypted message,
P
sequen e of numbers
is related to letter A),
(p ; p ; : : : ; p ), Q sequen e of numbers (q ; q ; : : : ; q ), Q sequen e of numbers (q ; q ; : : : ; q ) obtained as a y li shift of the sequen e Q k positions to the left (i.e. Q = (q ; q ; : : : ; q ; q ; q ; q )). 1
2
1
(k)
(3)
2
1
4
26
5
(k)
26
2
26
(k )
1
26
2
(k)
3
The previous method was based on the examination arried out for one pair of indexes i, j for whi h the value pi is the greatest among p1 ; p2 ; : : : ; p26 and the value qj is the greatest among q1 ; q2 ; : : : ; q26 . The key used for en ryption was expe ted to equal the number k , for whi h the maximum values in sequen es P and Q(k) appear at the same position.
p1 p2
pi q26 q 1 q 2
p26
qj
Now the main idea is to hoose su h a sequen e Q(k) among Q(0) ; Q(1) ; : : : ; Q(25) that it is the ` losest' to the sequen e P . In fa t the previous method is an example of this kind of method. In that method two sequen es were re ognized as similar if their maximum values appeared at the same position. What we are going to do now is to work out some better riterion of de iding whi h sequen e Q(k) is the losest to P . In order to do that we will nd some fun tion whose arguments are two sequen es and its value is a non-negative real number. We also demand that the value of su h a fun tion is lose to zero if and only if the sequen es are lose to ea h other in an intuitive sense. (In other words: we expe t that this fun tion would be a measure of similarity of the sequen es). Let us onsider the following example of su h a fun tion:
S (P; Q(
k)
)=
26 X
jq
i
(k)
p j: i
(1)
i=1
(
)
The fun tion S P; Q(k) re ognizes sequen es P and Q(k) as similar if the sum of dieren es between the
orresponding elements of these sequen es is small. At rst this fun tion may seem to meet our expe tations. Unfortunately, the following example shows that su h a denition of similarity of sequen es has some faults. For the purpose of simpli ation we shall onsider three sequen es whi h ontain ve elements.
P Q0 Q00 (Note that
2 3 4 5 5 = 19 ; 19 ; 19 ; 19 ; 19 1 2 3 4 9 = 19 ; 19 ; 19 ; 19 ; 19 2 3 4 9 1 = 19 ; 19 ; 19 ; 19 ; 19
Q00 is just a y li shift of Q0 .) S (P; Q0 ) = (1 + 1 + 1 + 1 + 4)=19 = 8=19 S (P; Q00 ) = (0 + 0 + 0 + 4 + 4)=19 = 8=19
As we an see the fun tion to P than Q00 .
S doesn't distinguish Q0 from Q00 although intuitively it is obvious that Q0 is loser
19
20
Cra k the Code
By reason of the above example we should improve our denition of the fun tion jqi0 pi j , whi h states the relative dieren e of formula (1) so that it uses the symbol i pi pi ):
=
26 X jq0 0 S (P; Q ) = p i
i
i=1
pj
p
i
i
=
26 X
p: i
X X jq0 Sb(P; Q0 ) = p 2 =
i
i
i=1
(2)
i
i=1
p j2
26
i
i
i
i=1
Now we an see that S(P, Q') an be interpreted as a weighted mean of Let us onsider the following modi ation of (2): 26
S . First we hange q0 and p (relative to
i
p
i
i
with weight fa tors
p. i
:
(3)
Here Sb is just a weighted mean of the squares of i . In this formula big relative dieren es between qi0 and inuen e the whole sum mu h more than in the ase of formula (2). Let us re all our latest example to he k whether the fun tion Sb works better than the fun tion S : Sequen es
Q0
and
Q00
p
i
are the same as in the previous example.
Sb(P; Q0 ) Sb(P; Q00 )
= (1=2 + 1=3 + 1=4 + 1=5 + 16=5)=19 4:48=19 = (0 + 0 + 0 + 16=5 + 16=5)=19 = 6:4=19
As we an see our new fun tion manages this example quite well. However there might appear some problems, when for some i the number pi was very small, for example less than = n (n is a number of letters used to
al ulate the sequen e Q, i.e. in the ase of Caesar's ode it is a number of all letters in the en rypted text, and in the ase of the se ond kind of ipher it is about of the total number of letters). Then it is quite likely that the value qi (k) (where k is the key whi h has been used for en rypting) won't be lose to pi , be ause of a small number of letters (small relative to =pi ) used for al ulating qi (k) . (It is a general problem we an't approximate the probability of some rare event if we perform only a small number of experiments in order to do that). Therefore we wish this kind of omponents of the sum (see formula (3)) not to inuen e this sum too mu h. The solution is to put several letters into one group and to treat them as a single symbol. E.g. in English ea h of the letters J, K, Q, X, Z appears very rarely, but the frequen y of o
urren e of J, K, Q, X and Z together may be big enough. Certainly, it auses some modi ations of sequen es P and Q. We an imagine that other elements p27 and q27 would be added to the sequen e P and Q respe tively and that appropriate elements would be removed from these sequen es (e.g. elements related to letters J, K, Q, X, Z). The number p27 would be a sum of pi whi h have been removed from P and q27 would be an analogous sum of some qi . (For the implementation details see the sour e ode of the program ra.pas). Now it is easy to apply the idea des ribed above in order to ra k the ode whi h is used by the program
ra k.pas ( ra k. ). We onsider ea h olumn of an array (see subse tion `En ryption program') separately. For the i-th olumn we al ulate the sequen e Q and hoose su h a number ki that the value of Sb P; Q(ki ) is minimal. After all that we should get the key k1 ; k2 ; : : : ; k10 . All it takes now is to ode our iphered message with the key k1 ; k2 ; : : : ; k10 in order to obtain the de iphered version of the message.
1 10
10%
1
(
)
(
)
(
)
About statisti al test 2 What we have nally obtained in formula (3) is a formula for so alled statisti s of the statisti al test 2 . The most ommon appli ation of this statisti al test is to verify whether some random variable (e.g. height of a human body) has a probability distribution similar to some given probability distribution (e.g. Gaussian distribution). In our task the probability distribution of the random variable was represented by the sequen e Q(k) and the given probability distribution was represented by P . Data sets
Five data sets were used to s ore the ontestants' solutions. It should be easy to see that a small number of letters in the en rypted message (or in the plaintext le) may ause some statisti al methods to give in orre t results. Therefore test ases with a small number of letters in their les an be more di ult to solve than others.
Cra k the Code No test ase
1 2 3 4 5
letters in ra*.in
891 1412 7422 404 225
letters in ra*.txt
460 1422 1756 170 100
21
Task by
Solution des ription by
Ville Leppanen
Tomasz Wale«
Postman A ountry postman has to deliver post to ustomers who live in villages as well as along every road onne ting the villages. Your task is to help the postman design a route that goes though every village and every road at least on e|the input data are su h that this is always possible. However, ea h route also has a ost. People in the villages wish that the postman visit their village as early as possible. Therefore, ea h village has made the following deal with the post: If the village i is visited as the k-th dierent village on the tour and k w( i), the village pays w( i) k euros to the post. However, if k > w ( i), the post agrees to pay k w ( i) euros to the village. Moreover, the post pays the postman one euro for ea h road on the tour. There are n villages, numbered form 1 to n. The post is lo ated in village number one, so the route should start and end in this village. Ea h village is pla ed on the rossing of two roads, on the rossing of four roads, or there is a road going through the village (i.e. there are 2, 4, or 8 roads going out of ea h village). There an be several roads onne ting the same villages or a road an be a loop, i.e. onne t a village with itself. Task
Your task is to write a program that:
reads the des ription of the villages and the roads onne ting them, from the input le pos.in, designs su h a route that goes through ea h village and road at least on e and maximizes the total pro t (or minimizes the loss) of the post, writes the result to the output le pos.out.
If there are several possible solutions, your program should output just one of them. Input
In the rst line of the input le pos.in, there are two integers n and m, separated by a single spa e; n, 1 n 200 , is the number of villages and m is the number of roads. In ea h of the following n lines there is one positive integer. The i + 1 -th line ontains w ( i), 0 w ( i) 1 000 , spe i ation of the fee paid by the village number i. In ea h of the following m lines there are two positive integers separated by a single spa e|villages onne ted by onse utive roads. Output
Your program should write one positive integer k, the length of the route, to the rst line of the output le pos.out. The following line should ontain k + 1 numbers of onse utive villages on the route v1 ; v2 ; : : : ; vk+1 , separated by single spa es, with v1 = vk+1 = 1 . Example
For the input le pos.in: 6 7 0 7 4 10 20 5 2 4 1 5 2 1 4 5 3 6 1 6 1 3
7
10
2
5 6
4
0 5
1
3
4
20
Postman
24
the orre t result is the output le pos.out: 7 1 5 4 2 1 6 3 1 Solution
The solution of this task is a little bit tri ky. The main observation is that the total prot/loss does not depend on the route hosen. Suppose that we have omputed some postman's route, su h that every edge is visited at least on e, vi1 ; vi2 ; : : : ; vil ; vi1 , l m, m is the number of edges. Let k j be the number of dierent villages visited before the rst visit to village number j . Then, the prot an be des ribed by the following formula.
()
profit =
X n
(w(j )
X l
k(j ))
j =1
1=
j =1
X n
w(i) +
n
w(i) +
i=1
Sin e the route should in lude every village, the sequen e write:
profit =
X
n
k(i) l
i=1
k(j ) is a permutation of the set f1 : : : ng, we an
n(n + 1)
l
2
i=1
X
For the given test data, every part of this equation ex ept l is onstant, so the total ost depends on the length of the hosen route, and not on the order in whi h the villages are visited. The value l is always grater P n(n+1) or equal to m, so the maximal profit equals ni=1 w i m. So we have to nd a route of length m, 2 whi h in ludes every edge exa tly on e. Lu kily for us, for su h graphs as given in the problem des ription, su h a route an always be onstru ted.
( )+
Solution of the task will go along the following s hema:
read the input data,
ompute a route whi h traverses ea h edge exa tly on e (su h a route is alled an Euler route), write the omputed route.
Euler route
Euler route is a very old and well known problem of the Graph Theory, it was posed in the XVIII entury by a famous mathemati ian L. Euler.
De nition An Euler Route of a onne ted graph G G does not ontain a vertex of degree 0.
a
= (V; E ) is a y le that traverses ea h edge of G exa tly on e. Graph
f
e
8
9
3 6
0
b
4 5
7 1
d
2
Img. 1. Example graph with marked Euler route: (a,b, ,d,e, ,f,b,e,f,a).
Theorem The Euler Route an be onstru ted if and only if the following onditions are satised:
the degree of ea h vertex is even, it is possible to onstru t a path between every pair of verti es.
If a graph satises these onditions, it is alled an Euler graph.
Postman Proof It is lear that if one of the above onditions is not satised, we annot onstru t an Euler route:
If it is not possible to onstru t a path between some pair of verti es, it is also impossible to onstru t a
y le whi h traverses every edge (the graph is divided into more than one distin t omponent).
()
If there is a v 2 V su h that deg v is odd, it is also impossible to onstru t an Euler y le. In any y le the number of edges entering a vertex is always equal to the number of outgoing edges, so the degree of any vertex should be even.
The aforementioned onditions are su ientthe following algorithm proves it.
Algorithm The algorithm is based on two fa ts:
for a given y le in G, if we remove it from G, it is still possible to onstru t an Euler y le in the remaining graph (or graphs). This omes from the fa t that removing a y le from G de rements the degrees of some verti es by even numbers, so all the ne essary onditions are still satised,
for given two distin t y les 1 , 2 with at least one ommon vertex, it is possible to onstru t a new y le
ontaining all the edges from 1 , 2 . This an be done by inserting the y le 2 into the y le 1 at the
ommon vertex.
1
2
Img. 2. Merging two y les.
This gives us the following pseudo- ode:
nd any y le in the graph (for example, using DFS), remove from the graph the edges belonging to this y le,
ompute an Euler route for the remaining graph (or subgraphs if removing the edges divides the graph into more than one omponent), merge the omputed y les into one y le and return it.
More areful implementation gives us the following algorithm: 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
pro edure EulerRoute(start); begin for v 2 adj(v) do if not marked (start,v ) then begin mark_edge(start,v ); mark_edge(v,start); EulerRoute(v ); Result.Push(start,v );
end end
Where Result is a sta k ontaining the edges of the Euler route. Running time of this algorithm is be ause every edge is traversed only on e; we need O m additional memory for marking edges.
( )
O(m + n),
25
26
Postman
Tests
Ten tests were used during the evaluation of ontestants' solutions. They are briey des ribed below. no 1 2 3 4 5 6 7 8 9 10
n 7 10 20 40 100 100 120 150 200 200
Remarks simple random test small random test `ladder' like test with some additional random edges random test loop random test `ower' like test (4 loops onne ted in 1 vertex) large random test `ladder' like test large random test
Task by
Solution des ription by
Adam Malinowski
Mar in Sawi ki
Knights We are given a hess-board of the size n n, from whi h some elds have been removed. The task is to determine the maximum number of knights that an be pla ed on the remaining elds of the board in su h a way that no two knights he k ea h other. x
x
x
x S
x
x x
x
Fig. 1: A knight pla ed on the eld S he ks the elds marked with x. Task
Write a program, that:
reads the des ription of a hess-board with some elds removed, from the input le kni.in, determines the maximum number of knights that an be pla ed on the hess-board in su h a way that no two knights
he k ea h other, writes the result to the output le kni.out.
Input
The rst line of the input le kni.in ontains two integers n and m, separated by a single spa e, 1 n 200 , 0 m < n2 ; n is the hess-board size and m is the number of removed elds. Ea h of the following m lines ontains two integers: x and y , separated by a single spa e, 1 x; y n|these are the
oordinates of the removed elds. The oordinates of the upper left orner of the board are (1,1), and of the bottom right
orner are ( n; n). The list of the removed elds does not ontain any repetitions. Output
The output le kni.out should ontain one integer (in the rst and only line of the le). It should be the maximum number of knights that an be pla ed on a given hess-board without he king ea h other. Example
For the input le kni.in: 3 2 1 1 3 3
the orre t result is the output le kni.out: 5
Solution
Graphs
=
Certainly, ea h reader is familiar with the following denition: a graph is an ordered pair G hV; E i, where V is an arbitrary set whose elements are alled verti es and E is a set of unordered pairs of elements of V the edges of G (thus an edge is a onne tion between two verti es). Most of you probably also know that a bipartite graph is su h a graph that its set of verti es V an be represented as a disjoint union of two sets, V X [ Y , X \ Y ;, where all edges have one end in X and the other in Y . Bipartite graphs are often represented as ordered triples G hX; Y; E i, though they also fulll the usual denition of a graph.
=
=
=
Knights
28
A graph (on the left) and a bipartite graph (on the right). The points represent the graphs' verti es and the lines represent the edges.
Independent set of verti es For any graph G = hV; E i, we an pose the following question: what is the maximal number of elements of a subset I V su h that no two elements of I are onne ted by an edge? (Su h a set is alled an independent set
of verti es.) This problem orresponds to a situation where the edges represent an ex lusive hoi e among the verti es: one of them might be sele ted, but not both. It is exa tly what we have in our problem: if we build a graph whose verti es are elds of the hess-board and edges represent all possible single moves of a knight, then we have to al ulate the maximal ardinality of an independent set of verti es. Please note that the graph obtained is a bipartite one, sin e a knight always jumps from a white eld to a bla k one and vi e-versa. 1 2 4
1
2
3
4
5
7
8
3 6
6
5 8 7 9
10
11
10 9 11
An example 4 4 hess-board with several elds removed and the orresponding bipartite graph of knight's moves.
The problem of a maximum independent set has its twin: minimum vertex over. In this problem, we need to nd the minimal ardinality of a set C of verti es su h that ea h edge is ` overed' by at least one element of this set, that is at least one end of ea h edge falls into this set. It is an easy exer ise to show that a omplement of a vertex over is an independent set and vi e-versa, hen e a omplement of a minimum vertex over is a maximum independent set. This means that if a graph has n verti es, n jV j, then jI j n jC j, where C is the minimum vertex over and I is the maximum independent set. It is a well-known fa t that the problems of the maximum independent set and of the vertex over for arbitrary graphs are NP- omplete, hen e it is not known how to solve them in a polynomial time, or even if it is possible or not. For further information on this topi see e.g. [1℄. However, we have already mentioned that we only onsider bipartite graphs, not arbitrary ones. Lu kily, in this ase there exists a su iently fast solution. To nd it, we need to learn a bit about mat hings.
=
Mat hing
=
=
Finding the maximum mat hing is another graph problem of great importan e. In a graph G hV; E i, a mat hing M is a subset of E su h that no two dierent edges belonging to M have a vertex in ommon. The problem is of ourse to nd the maximal number of elements of a mat hing in a given graph.
An example of a maximum mat hing in a bipartite graph.
C
In a bipartite graph, there exists a onne tion between the maximum mat hing M , minimum vertex over and the maximum independent set I . It turns out that the ardinality of the maximum mat hing exa tly
Knights =
=
equals the ardinality of the minimum vertex over, jM j jC j, hen e jI j jV j jM j. This equality, known as the `Hungarian theorem' (by König and Egerváry), will be dis ussed later. Now, let us on entrate on nding the maximum mat hing. Finding the maximum mat hing in general graphs an be done in time O n5=2 . The algorithm is by Even and Kariv and is des ribed in [2℄. However, for bipartite graphs we know a mu h simpler and at least equally fast algorithm, namely the Hop roft-Karp algorithm. Let us present it briey.
(
Augmenting path
)
=
Consider any non-maximum mat hing M in a bipartite graph G hX; Y; E i. If a vertex is not mat hed, i.e. it is not an end of any of the edges belonging to M , than we shall all it free. Sin e G is bipartite, every path in this graph has even verti es in X and odd in Y , or vi e-versa. An alternating path is a loop-free path in G starting at a free vertex in X and su h that every even edge in this path belongs to M (hen e every odd edge does not). If it also ends in a free vertex in Y , we all it an augmenting path. An observant reader ould have already guessed where the name of an augmenting path omes from. If we ex hange mat hed and unmat hed edges along an augmenting path, we obtain a mat hing that has one more edge.
An augmenting path in a bipartite graph.
Of ourse, if a mat hing is a maximum one, there are no augmenting paths. Lu kily, the opposite also holds: if there are no augmenting paths, we have the maximum mat hing. (This fa t is known as Berge's theorem and an be proven e.g. by onsidering the `ex lusive or' of a maximum mat hing and a mat hing that has fever elements.) This leads us to an algorithm for nding a maximum mat hing: sear h the graph for augmenting paths as long as there are any. If we use the breadth rst sear h (BFS) method to nd the shortest augmenting paths, then the duration of the entire omputation would be O n n m , where n jV j and m jE j1 , sin e BFS takes O n m steps and the size of the mat hing (that is, the number of augmenting paths found) is of ourse not greater than n2 . In our ase, the arity of ea h vertex is 8 or less, hen e we have at most verti es and edges. It leads to approximately : 9 steps.
( ( + ))
( + ) 160 000
=
=
40 000
6 4 10
The Hop roft-Karp algorithm The idea of the Hop roft-Karp algorithm is to enlarge the mat hing not by a single shortest augmenting path, but at on e by the maximal set of vertex-disjoint augmenting paths of the shortest possible length. (Please note that by the maximal set we mean su h a set of augmenting paths that no other augmenting path of the same length is disjoint with its members, so the set annot be extended by adding any path, whi h does not imply that the set has the maximal possible number of elements.) How to do it ee tively? Assume G hX; Y; E i is the graph, M is the mat hing to be augmented and l is the length of the shortest possible augmenting path. First, use BFS to build an auxiliary dire ted graph H as the union of all alternating paths of length less or equal to l (not ne essarily disjoint). Note that if x 2 X and y 2 Y are free in M , then a path from x to y in H is an augmenting path of length l (if only it exists). So now we an repeat the depth rst sear h (DFS) in H to nd out onse utive augmenting paths and enlarge M using ea h of them in turn. Sin e we mark out every visited vertex, the paths found are pairwise disjoint indeed. The BFS phase takes at most O n m time units. DFS is repeated many times, but it examines ea h vertex at most on e, so it also takes O n m steps to a
omplish. Hen e we an have many p augmenting paths at the same pri e as only one! Moreover, it an be proven that in su h ase we only need O n repetitions of p the above pro edure to obtain the maximum mat hing. Hen e the total omputation time is O n n m . In our ase, where n an be as large as , it leads to a solution times faster than the straightforward approa h presented before.
=
( + ) ( + )
40 000
n
( )
200
1 Please note that we use the standard notation: n for the number of verti es and and m have a dierent meaning in the problem `Knights'.
m
( ( + ))
for the number of edges in a graph, though
29
30
Knights
`Hungarian theorem' Now we already know how to solve the problem `Knights', sin e we only need to al ulate the size of the maximum independent set. It equals jV j jM j. However, what would we do if we were to a tually determine the independent set of maximal size, i.e. the positions of the knights on the hess-board? Let us assume that we have a bipartite graph G hX; Y; E i. Let M be the maximum mat hing. Consider the union A of all sets of verti es of alternating paths in G. (Re all that an alternating path starts in a free vertex of X and rst follows an unmat hed edge, then a mat hed one et .) It turns out that C X nA [ Y \A is the minimum vertex over. Indeed, one an prove that C is a vertex over (the proof is rather straightforward from the denition). Then we noti e that no free (unmat hed) verti es belong to C (this would ontradi t the fa t that M is a maximum mat hing) and that for every edge e 2 M , at most one of its ends belongs to C . Hen e jC j jM j. But every edge belonging to M has to be overed, and sin e these edges have no verti es in ommon, then there must be at most jM j verti es in any vertex over, in parti ular in C . Hen e C is a minimum vertex over, and its omplement I is a maximum independent set.
=
=(
) (
)
On the lefta graph with a maximum mat hing and only one alternating path. The set A of its verti es is denoted by ir les. In the middlea minimum vertex over of the graph. On the righta maximum independent set. Other solutions
All reasonable solutions of the problem are based upon the `Hungarian theorem' and lean on nding the maximum mat hing. We have already mentioned the simple algorithm for maximum mat hings of the omplexity O n n m . If we give up nding the shortest augmenting path, we an use DFS instead of BFS to nd the paths and use the rst path found. Due to high regularity of the graph in our problem, if we always start our sear h at the vertex next to the last vertex visited in the previous sear h, we have a good han e to nd a really short augmenting path in just several steps. It is mu h heaper than performing an exhaustive sear h of the entire graph to make sure that our path is as short as possible. In fa t, this simple heuristi often works as fast as the sophisti ated Hop roft-Karp algorithm or even faster, and ould s ore a maximal number of points.
( ( + ))
Tests
There were 10 input sets, among whi h only 3 were really large. Ea h of these tests was worth equal number of points. The following table des ribes briey all of them (please note that now n and m denote the length of the edge of the hess-board and the number of elds missing respe tively, hen e jV j n2 m).
=
n
m
jV j
no 1 2 3 4
2 2 10 20
0 1 67 200
4 3 33 200
5 6 7 8 9
28 40 80 180 200
276 913 4233 14632 4
508 687 2167 17768 39996
10
200
10100
29900
des ription full board board with one eld missing board, random elds missing with regular uts along two edges and some random elds missing as the previous one, as the previous one, as the previous one, as the previous one, with four elds near the orner missing (in the shape of letter L) , random elds missing
2 2 2 2 10 10 20 20
200 200 200 200
28 40 80 180
28 40 80 180
Task by
Wolfgang Pohl
Solution des ription by
Tomasz Wale«
Mars Maps (This task was inspired by task `Atlantis' of the MidCentral European Regional ACMICP Contest 2000/2001.)
In the year 2051, several Mars expeditions explored dierent areas of the red planet and produ ed maps of these areas. Now, the BaSA (Balti Spa e Agen y) has an ambitious plan: they would like to produ e a map of the whole planet. In order to al ulate the ne essary eort, they need to know the total size of the area for whi h maps already exist. It is your task to write a program that al ulates this area. Task
Write a program that:
reads the des ription of map shapes from the input le mar.in,
omputes the total area overed by the maps, writes the result to the output le mar.out.
Input
The input le mar.in starts with a line ontaining a single integer N (1 N 10 000 ), the number of available maps. Ea h of the following N lines des ribes a map. Ea h of these lines ontains four integers x1 , y1 , x2 and y2 (0 x1 < x2 30 000 , 0 y1 < y2 30 000 ). The values ( x1 ; y1 ) and ( x2 ; y2 ) are the oordinates of, respe tively, the bottom-left and the top-right orner of the mapped area. Ea h map has re tangular shape, and its sides are parallel to the x- and y -axis of the oordinate system. Output
The output le mar.out should ontain one integer A, the total area of the explored territory (i.e. the area of the union of all the re tangles). Example
For the input le mar.in: 2 10 10 20 20 15 15 25 30
the orre t result is the output le mar.out: 225
32
Mars Maps
Solutions
The task `Mars Maps' has many possible solutions. As we shall see, some of them are very simple. We will present them starting with a simple but ine ient one and we will gradually improve the solutions' e ien y.
Solution 1
30 000 30 000
Let us suppose, that we have an array of Booleans of size , and we treat it as a big 2D grid. We
an `draw' all the given maps on it and then ount the area overed. This is a straightforward solution and it
an be easily implemented. However, it requires a huge amount of memory (almost 100 MB) and pro essing 2 power (time omplexity an be as big as n ). It an be very inee tive even for small tests (e.g. those with maps overing almost the whole area). Figure 1 shows a sample array generated by this solution.
30 000 ( + 2)
Fig. 1. Solution 1, drawing on the grid.
Solution 2 How an we improve the previous solution? The memory onsumption should denitely be smaller and the solution should work faster (at least) for small tests. The main observation is that not all oordinates are used in the omputations. The improved solution onsists of two steps: The rst step is reating a grid onsisting of used oordinates only (i.e. oordinates of maps' orners), it is presented in g. 2. The se ond step is exa tly the same as in the previous solution: we draw the maps on the prepared grid. This time the omputation of the overed area is slightly dierent. In the previous solution, the grid onsisted of square elds . In this
ase, the area of ea h eld of the grid has to be omputed from the parameters of the grid. This solution is slightly better, it does not depend on the range of the oordinates, but of ourse for large data it is still ine ient. The time omplexity of this solution is O n3 (sin e the ` ompressed' grid might be as large as n n).
1 1
( )
Fig. 2. Solution 2, ompression of oordinates.
Solution 3 We an further improve the e ien y using a te hnique alled `s anline sweeping'. It is based on s anning obje ts (maps) on a plane in ertain order. In the previous solutions, the order was not important, but here we will make use of pro essing maps with an in reasing x oordinate. We an s an the plane of the Mars's surfa e with a verti al line (often alled a `brush'), from left to right. Ea h time the line en ounters the left edge of some map, we mark it on the brush, and when the right edge is en ountered we unmark it. For ee tive implementation, we need a fast method of omputing the total length of marked edges on the brush. Between subsequent edge en ounters, the total length of the sum of marked edges is onstant, so the map's area in this segment an be easily omputedit is BrushLength x, where x is the distan e between x
oordinates of subsequent edges en ountered by the brush (see Fig. 3).
Mars Maps
Fig. 3. S an-line solution, the dashed line shows the previous position of the brush, the solid line shows the
urrent one, and the darker area shows the maps' area lying between the two onse utive maps' edges.
Data stru tures For the aforementioned algorithm, we need a data stru ture whi h implements the following operations:
MarkEdge(a; b)marking the edge [a; b℄, on the brush, UnmarkEdge(a; b)unmarking the edge [a; b℄ on the brush (we an assume that su h an operation always su
eeds, i.e. the brush ontains the given edge),
T otalLength omputing the total length of the sum of all marked edges.
= 30 000
Let us denote by max_ the maximal value of the oordinates (in our ase max_ ). From the problem spe i ation, a and b are integers in the range : : : max_ . The simplest implementation of the above data stru ture, is an array of integers of size : : : max_ . Ea h element of this array orresponds to one unit interval, and is equal to the number of marked edges ontaining it. The implementation of MarkEdge a; b and UnmarkEdge a; b is fairly simple: elements of the array in the range a; b need only to be in remented or de remented by one. However, su h an implementation is quite slow. In the pessimisti ase it requires O max_ operations. Nevertheless it is an improvement ompared to the previous solution, however it an be improved even further. The way to do it is to use balan ed trees. We an build a tree over the range ::max_ in su h a way that its leaves orrespond to one unit intervals, while the internal nodes orrespond to the intervals being the sum of the intervals of their des endants.
[0
[
( ) (
1℄
℄
[0
℄
( )
)
[0
℄
[1,10℄
[1,5℄
[6,10℄
[1,3℄
[1,2℄
[1℄
[4,5℄
[3℄
[2℄
[4℄
[6,8℄
[5℄
[6,7℄
[6℄
[9,10℄
[8℄
[9℄
[10℄
[7℄
Fig. 4. An example of a balan ed tree for the range [1..10℄.
Ea h node ontains two values: Lengththe length of the overed part in the node's orresponding interval, and Countthe number of edges overing it. The T otalLength operation returns simply the value of Length of the tree root. The other operations are a bit more omplex. We try to avoid operating on small intervals: ea h time we dis over an operation that would
ause hanges in all the leaves of a node's subtree, we only need to hange the values in the node. More pre isely, we follow the following s hema:
[a; b℄ we start from the root (node number 1). Let v be the urrent node, orresponding to the interval [l; r℄, and let v 0 and v 00 be its dire t des endants,
orresponding to the intervals [l; m℄ and [m + 1; r℄ respe tively (where m = (l + r) div 2). We may For a given edge
en ounter the following ases:
33
Mars Maps
34
the interval [l; r℄ is fully ontained in [a; b℄in su h a ase we modify the values in the urrent node
v and return to its parent, the intervals [l; r℄ and [a; b℄ interse tif [l; m℄ and [a; b℄ interse t, we des end to the left hild v 0 , if [m +1; r℄ and [a; b℄ interse t, we go to the right hild v00 . It may be ne essary to go in both dire tions, however it will not happen too often as we will see later on. After visiting the hildren we return to the parent.
the intervals [l; r℄ and [a; b℄ do not interse tthis situation is impossible, sin e we never des end to su h a node.
Moreover, ea h time we return from a hild node it is ne essary to re ompute the value of Length from the values stored in the hild nodes. Computing the Length value is rather straightforward: if the node's Count is positive, then the Length value is equal to b a, otherwise it equals v0 :Length v00 :Length. We an implement these operations in the following way:
+1
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
1: 2: 3: 4: 5: 6: 7: 8: 9:
+
pro edure modifyInterval (node, y0, y1, node_y0, node_y1, delta) begin if (y0 node_y0 ) and (y1 node_y1 ) then begin v [node℄=v [node℄+delta;
end else begin
m:= (node_y0 +node_y1 ) div 2; if (y0 m) then modifyInterval (node*2, y0 , y1 , node_y0 , m, delta); if (m0) then sum[node℄ := 1+node_y1 node_y0 ; else begin sum[node℄ := 0; if (node_y0 <node_y1 ) then sum[node℄ := sum[node*2℄+sum[node*2+1℄ end; end;
(log
)
We still have to prove that the pro edure modifyInterval runs in time O max . It may happen that a des ent to both the left and the right node will be needed, but it auses some additional ost only when it happens for the rst time. After that rst `split', every following one is very simple: the ost of pro essing one of the hildren is onstant, sin e we return to the parent immediately after des ending to it (see g. 5). The time omplexity omes from the observation that the total height of the tree is equal to O max .
(log
)
[1,10℄
[1,5℄
[6,10℄
[1,3℄
[1,2℄
[1℄
[4,5℄
[3℄
[2℄
[4℄
[6,8℄
[5℄
[6,7℄
[6℄
[9,10℄
[8℄
[9℄
[10℄
[7℄
Fig. 5. The result of adding the interval [2; 8℄, visited nodes are oloured gray, and those with modied values are darkened.
Mars Maps The Algorithm Having implemented an ee tive data stru ture, the main algorithm might be written in the following way: 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
last_x := 0; for e GetLeftmost Edges do begin (x , y0 , y1 , sign) := e; area := area+sum[1℄*(x -last_x ); last_x := x ; if (sign = +1) then modifyInterval (1, y0, y1, 0, max_x,
=
(
)
else
modifyInterval (1, y0, y1, 0,
end;
+1); max_x, 1);
The variable Edges ontains an ordered sequen e of verti al edges of Mars Maps; it is ordered by the as ending x oordinate and (for the edges with the same oordinate) by the sign (+1 for left edges, for right edges). Sin e we have the data stru ture whi h implements the pro edure ModifyInterval with omplexity O max , and in the whole run of the algorithm it will be alled exa tly n times, then the total time
omplexity is O n max .
1
(log
)
( log
2
)
Tests
Ten test data sets were used during the evaluation of the ontestants' solutions. They are briey des ribed below. no 1 2 3 4 5 6 7 8 9 10
n 4 7 13 100 200 1000 2000 5000 10000 10000
Remarks simple test simple test image omposed of re tangles with text: `BOI' random re tangles snail like shape random re tangles large re tangles pla ed in X shape many verti al and horizontal bars two groups of squares rhombus shape
35
Task and solution des ription by
Krzysztof Onak
Teleports Great sor erer Byter reated two islands on the Balti See: Bornholm and Gotland. On ea h island he installed some magi al teleports. Ea h teleport an work in one of two modes:
re eiving|one an be teleported to it, sending|anyone who enters the teleport is transfered to the spe i destination teleport on the other island, provided that the other teleport is in the re eiving mode.
On e, Byter gave his apprenti es the following task: they must set the teleports' modes in su h a way, that no teleport is useless, i.e. for ea h teleport set in the re eiving mode there must be at least one teleport sending to it, set in the sending mode; and vi e versa, for ea h teleport set in the sending mode, the destination teleport must be set in the re eiving mode. Task
Write a program that:
reads the des ription of the teleports on both islands, form the input le tel.in, determines the appropriate modes for the teleports, writes the result to the output le tel.out.
If there are several possible solutions, your program should output just one of them. Input
In the rst line of the text le tel.in, there are two integers m and n, 1 m; n 50 000 , separated by a single spa e; m is the number of teleports on Bornholm, and n is the number of teleports on Gotland. Teleports on both islands are numbered from 1 to m and n respe tively. The se ond line of the le ontains m positive integers, not greater than n, separated by single spa es|the k-th of these integers is the number of the teleport on Gotland that is the destination of the teleport k on Bornholm. The third line ontains analogous data for the teleports on Gotland, i.e. n positive integers, not greater than m, separated by single spa es|k-th of these integers is the number of the teleport on Bornholm that is the destination of the teleport k on Gotland. Output
Your program should write two lines des ribing the modes of the teleports, respe tively, on Bornholm and Gotland, to the output le tel.out. Both lines should ontain a string of, respe tively, m and n ones and/or zeros. If the k-th hara ter in the string is 1, then the k-th teleport is in the sending mode, and if it is 0, then the orresponding teleport is in the re eiving mode. Example
For the input le tel.in: 4 5 3 5 2 5 4 4 4 1 3
Bornholm
Gotland 1
1 2 2 3 3 4 4 5
the orre t result is the output le tel.out:
38
Teleports
0110 10110
Bornholm
Gotland 1
1 2 2 3 3 4 4 5 Solution
Let us start with an easy observation. Sometimes input data determine modes of some teleports even when there are many possible solutions (we will see later that a solution always exists). For example if there is a teleport on one of the islands, for whi h there is no teleport on the other island that ould send anything to it, we know that su h a teleport must be in the sending mode, otherwise it would be useless. Teleport no. 1 on Gotland in the example input data (g. 1) is just su h a devi e.
Bornholm
Gotland 1
1
2
2
3
3
4
4
5
Fig. 1. Teleports in dashed ir les are for ed to be in the sending mode due to the la k of teleports that
ould send to them.
Moreover its destination teleport (no. 1 on Bornholm) must be in the re eiving mode due to the requirements of the task. Besides, it is entailed that no teleport in the solution an send to devi e no. 3 on Gotland, so it must be in the sending mode and so on. Certainly, this simple fa t and its further onsequen es will not help us to nd a solution in every situation. See gure 2 for an example.
Gotland
Bornholm
1
1
2
2
3
3
4
4
Fig. 2. None of the teleports is determined to be in a spe i mode.
There are four teleports on ea h island and for ea h of them there exists a devi e sending to it. Besides, there are exa tly two solutionsall teleports on Gotland are in the sending mode and all on Bornholm in the re eiving mode and vi e versa. It means that none of the teleports is determined to be in a spe i mode. In spite of this our previous observation is not useless.
Teleports Algorithm
Let us try a simple method. First we set all teleports on Bornholm in the re eiving mode, and all teleports on Gotland in the sending mode. Of ourse it does not have to be a solution, but we will try to repair it by looking for teleports for whi h there is no devi e sending to it as we have done before while analyzing the problem. Here is the algorithm: 1: 2: 3:
set all teleports on Bornholm in the re eiving mode; set all teleports on Gotland in the sending mode; while there is a useless teleport x in re eiving mode on Bornholm do
begin
4: 5:
swit h x to sending mode; swit h destination teleport of
6:
end
7:
x
to re eiving mode;
Why does it terminate? With every step of the loop in lines 37 the number of teleports in the re eiving mode on Bornholm de reases, and be ause it is always nite, at some point there will be no devi e x as spe ied in line 3. Why is a state of teleports in row no. 8 a solution? There may be four groups of teleports and we will show that devi es in ea h group annot be useless.
Re eiving mode, Bornholmthe algorithm has stopped, so due to the ondition in the fourth line none of them is useless. Sending mode, Bornholmthe destination teleport of every su h a devi e has been swit hed to the re eiving mode (row no. 6) and its state has not been altered anymore. Re eiving mode, Gotlanda devi e is in this mode, be ause it was a destination teleport for some teleport on Bornholm that has been swit hed to the sending mode, so it is not useless. Sending mode, Gotlanddestination teleports of theses devi es were initially set in the sending mode and have not been altered.
This proves the orre tness of the algorithm. Now we have to implement our solution hoosing appropriate data stru tures. Implementation
Our program has to write to the output le a number of hara ters proportional to the total number of teleports on both islands. It means that the time omplexity of our solution annot be better than n m . Later on, we will see that su h a omplexity an be a hieved. We will use the same names as in the example program. At the beginning we read numbers n, m and destinations of all teleportswe store them in the array dest so that we an get the destination devi e for a spe i teleport in onstant time. In the array sending we keep the modes of teleports. It is initialized a
ording to lines 12 of the algorithm. For ea h teleport on Bornholm we ount for how many devi es on Gotland it is a destination and store this information in the array inDegree. Then, we prepare an empty stru ture zeroDeg whi h provides the following operations: inserting an item into the stru ture, getting out any of the items previously inserted and testing if the stru ture is empty. Ea h of the operations should take onstant time. This stru ture an be implemented for example as a sta k or a queue. We put all the teleports on Bornholm that are urrently useless, these are the ones for whi h inDegree is zero, into zeroDeg. All the steps done so far require n m time. Now, the loop in lines 37 starts. Che king if there is a useless teleport on Bornholm is simply he king if zeroDeg is empty. If it is, we have nished, otherwise we take a teleport number from zeroDeg. Let us all this teleport x. We swit h x to the sending mode and we set its destination devi e y in the re eiving mode. Next we de rease inDegree for the destination z of the teleport y . If z be omes useless, it means that inDegree for z has rea hed 0 and we put z into zeroDeg. It is lear that every exe ution of the loop takes O time, and be ause it happens at most m times, we know that the time omplexity of this part of the algorithm is O m . Hen e, the program needs n m memory and time and a
ording to the previous observation it is optimal.
( + )
( + )
(1)
( + )
( )
39
Teleports
40
Inspiration
( ) : ( ) ( ) f~(A) = ff (a)ja 2 Ag
For an arbitrary set X let us denote by P X the set of all subsets of X , Y and a fun tion f X ! Y a fun tion f~ P X ! P Y as:
:
X.
Now let us dene for arbitrary sets
for all A X . As an author of the problem I an reveal that it was inspired by the theorem alled `Bana h's lemma'. It says what follows: For arbitrary fun tions that
f :A!B
and
g : B ! A there exist sets A1 ; A2 A and B1 ; B2 B
su h
A [ A = A, A \ A = ;, B [ B = B , B \ B = ;, f~(A ) = B ; ~g(B ) = A : 1
2
1
2
1
2
1
2
1
1
2
2
Speaking less formally it says that we an divide both A and B into two parts (respe tively A1 , A2 and B1 , B2 ) su h that applying f to A1 we get exa tly B1 and applying g to B2 we get A2 (see gure 3).
f
A A
B B
1
1
A
B
2
2
g Fig. 3. The presentation of the idea of Bana h's lemma.
We an treat Bornholm and Gotland as sets in luding teleports. Then relation origin-destination is a fun tion in both dire tions. What is more, we have proved Bana h's lemma for nite sets A and B . It is mu h harder to show that it is true even when both sets are innite. Tests
There were 10 tests used to evaluate ontestants' programs. Most of them were random and a few were prepared against some simple heuristi s and programs with worse omplexity. No 1 2 3 4 5 6 7 8 9 10
m
10 100 1 000 1 000 10 006 10 000 50 000 1 000 50 000 50 000
n
10 107 1 005 1 000 10 000 10 000 1 000 50 000 49 987 50 000
Bibliography [1℄ Thomas H. Cormen, Charles E. Leisereson, Ronald L. Rivest. Introdu tion to Algorithms. The MIT Press, 1990.
(
)
[2℄ S. Even, O. Kariv. An O n2:5 algorithm for maximum mat hing in general graphs. In Pro . 16th Annual Symp. on Foundations of Computer S ie e, pages 100112. IEEE, 1975.
41