A binary tree is a structure composed of individual nodes. One of them is special - it is called root. Each node either has exactly two descendants (some other nodes), or no descendants (then it is called leaf). The depth of a node is its distance from the root. Note that root can be a leaf - then the tree consists of a single node of depth 0. An example of a binary tree is shown on the following picture:
In order to describe binary trees in an easy manner, we will introduce the following coding: k-th row of the coding (for k=0,1,2...) describes the nodes of depth k from left to right. Those nodes which are not leaves will be represented by a letter N, leaves will be represented by a letter L. A binary tree from the previous figure will be coded as:
N NN LNLL LL
Given is the number of leaves M (M <= 10 000) and their depths in a binary tree (M integers). Write a program, which will generate a binary tree with leaves of given depths and print it in our coding. If there are more binary trees that agree with the input data, print an arbitrary one. If there is no binary tree for a given input, the program should inform about it.
Input format: First row of the input file trees.in contains a single number M (number of leaves). The second line contains M numbers - depths of the leaves of a binary tree.
Output format: The output file trees.out should contain a code of a binary tree in a format described above, or a message Such binary tree does not exist.
Example:
Input:
trees.in 4 2 3 1 3Output:
trees.out N NL LN LLInput:
trees.in 3 1 1 2Output:
trees.out Such binary tree does not exist.
The enemy armies started to attack the kingdom of king Lovepeace III. They alrady conquered and settled in several cities. The king needs to give a command to his general to fight back (without a command, the general cannot fight). However, the general is making an inspection of the army in a different city. Therefor, it is necessary to send a messenger, which will deliver the command as soon as possible. Anyhow, the command may not get into the hands of the enemy! The messanger must keep as far as possible from the cities captured by the enemy. Can you find the best path for the messenger?
Given is the number of cities N (1 <= N <= 100) on the input. Individual cities are identified by numbers 1...N. The input follows by the number of paths M (1<= M <= 10000) and a list of these paths interconnecting the cities. Each path is determined by a pair of cities. The paths intersect only in cities and for any two cities, there exists a sequence of paths using which one can travel from one to the other. The following number K on the input specifies the number of cities captured by the enemy, and it is followed by a list of captured cities. Finally, the program will receive a number of the city where the messenger starts and number of the city where the general is located. Your program should find a trajectory, which has a maximum distance from enemy cities. If there are more such trajectories, the program should find an arbitrary shortest one. The distance of cities A and B is defined as a minimum number of cities one has to pass to get from city A to city B. The distance of a trajectory from a city A is the smallest from the distances between cities on that trajectory from a city A, or zero if A is on that trajectory.
Input format: The first line of the input file general.in contains numbers N (number of cities) and M (number of paths). It is followed by M rows, each of them describing one path. A path is described by a pair of city numbers that the path interconnects. The following line contain the number of captured cities K followed by K lines with numbers of captured cities. The last line of the input file contains the number of a city where the messenger starts and the number of a city where the general is located.
Output format: The output file of the program - general.out will contain numbers of cities on the best trajectory in the direction from messenger start to general. All numbers should be placed in the same line and separated by spaces.
Example:
Input:
general.in 10 12 1 2 2 3 3 4 4 5 2 5 1 6 6 7 7 8 8 5 1 9 9 10 10 5 1 3 1 5Output:
general.out 1 9 10 5
A group of friends decided to go together to a bicykling trip. The trajectory they chose leads through several national parks and thus they can stay overnight only at legal camping places. They are about to decide in which campings they will stay.
The total length of the planned trajectory is L (1 <= L <= 1 000 000 000). A maximum distance our friends can travel in one day is K (i.e. in two consequetive days, they have to stay in campings that are not more distant than K). There are total N campings on the planned trajectory (0 <= N<= 10 000); i-th camping is in the distance l[i] from the start of the journey and the price to stay there is c[i] (1 <= c[i] <= 20 000). The numbers L, K, l[i] and c[i] are positive integers; all l[i] are different and 0<l[1]<l[2]<...<l[N]<L. Your task is to decide whether the group can make this journey at all. If yes, find the campings they have to stay overnight so that
Input format: The input file trip.in contains space-separated numbers L, K a N on the first line. The following N lines contain space-separated pairs of numbers l[i] and c[i], for i=1..N
Output format: The output file trip-a.out for the task a) and trip-b.out for the task b) will either contain a single line with sentence "Trip is impossible.", or the first line will contain two numbers- M and C. The first one, M (0<= M), is the number of campings where the group will stay overnight, the second, C, is the price which the group will pay for staying overnight in all of them. The second line of the output will contain M space-separated camping numbers, where the friends will stay. Campings are numbered from 1. If M=0, the second line can be omitted. If there are several possibilities satisfying the condition a) or b), program should print an arbitrary one.
Example:
Input:
trip.in 25 5 9 4 2 5 8 8 2 10 8 12 2 15 8 16 2 20 8 24 2Output:
trip-a.out 4 32 2 4 6 8 trip-b.out 5 16 1 3 5 7 8Input:
trip.in 15 10 2 2 11 4 12Output:
trip-a.out, trip-b.out The trip is impossible.Input:
trip.in 8 10 1 7 11 trip-a.out, trip-b.out 0 0
Let's start with several definitions: Tiles are squares of an equal size with colored edges. A particular assignemt of colours to edges is called type of tile and is represented by an ordered quadruple (l,p,h,d) specifying the color of left, right, top, and bottom edges in this order. To simplify our work, we will use different symbols - letters, digits, etc. instead of regular color names. For example a tile of type (1,2,3,4) looks like this:
The space we want to tile (called wall) has a shape of a rectangle of size m times n (both m and n are natural numbers; the unit is the length of the edge of one tile). The sides of the rectangle are divided into segments of unit length. Each segment is assigned a color. Our goal is to tile the wall with the tiles in such a way that each of the m*n unit squares of the wall will contain exactly one tile, the neighboring tiles will be facing each other with edges of the same color and the tiles on the sides will be facing the sides of the rectangle always with the edge of the same color as is the color of the corresponding segment of the wall border. The tiles can not be rotated.
Example:
Tiling can be easily used to solve tasks, in which the result is a simpe "yes" or "no" answer. To transform such a task into a tiling problem, we can construct a suitable set of tile types (these are independent from the input, and are fixed for a given problem). Then we color the segments of one side of a sufficiently large wall with colors according to the problem input. The remaining sides are painted with one color and the the transformed task is whether this wall can be tiled or not. The result should be the same as is the result of the corresponding task.
The width of the wall will be always the same as is the length of the input to the original problem. The height of the wall will be the lowest possible, for which there exists a tiling using the designed set of tiles.
This way of computation resembles traditional programming. The designed set of tiles corresponds to a computer program and the required height of wall corresponds to the running time of the program - thus we will try to minimize it in our solutions.
More formally, a tiling program is an ordered quadruple D=(T,l[0],p[0],d[0]), where T is a finite set of tile types {(l[1],p[1],h[1],d[1]), ...,(l[k],p[k],h[k],d[k])} and l[0], p[0] and d[0] are border colors. A decision problem P(x) is a task to determine whether the input x (a finite sequence of symbols (i.e. colors) from a predetermined finite set of symbols) has the required property P. A tiling program solves a decision problem P(x), if P(x)="yes" if and only if there exists such v>0 that it is possible to tile a wall with dimensions |x| times v using tile types from the set T, where the top side of the wall is painted correspondingly to the input x, and the left, right, and bottom sides are painted by colors l[0], p[0] and d[0] in this order. It is possible to use as many tiles of each type as needed. The complexity of a tiling program D for a given input x is the lowest v, for which the tiling is possible; if such v doesn't exist, and thus P(x)="nie", the complexity is defined to be zero. The complexity of the program is a function of the input length n, and its value is the maximum of all complexities of tiling programs for different inputs of the length n.
Example:
Let's try to construct a tiling program, which will determine whether a given sequence
of natural numbers x[1],...,x[n]
(0<= x[i]<= 9) is nondecreasing. We will use tiles of the following
types:
the left border will be painted with color 0, the right with a color "circle", the bottom with a color "dot". We state that this program solves the given task with complexity O(1). However, it is necessary to prove this.
We will first show that the height of each wall, which can be tiled with tiles of types from the set T, is 1. This is clearly implied from the fact that the color on the bottom side of each tile is "dot", which occurs at the top side of no tile type. For the same reason, the tiles with "circle" on their right edge must be facing the right side of the wall. Thus each correct tiling must look like this:
However, this is possible if and only if 0<= x[1] <= x[2] <= ... <= x[n-1] <= x[n], i.e. the sequence on the input is nondecreasing.
a) Construct a tiling program, which will decide about a given sequence of zeros and ones, whether it represents a binary representation of some natural number divisible by 5.
b) Construct a tiling program, which will decide whether a given sequence of natural numbers x[1],...,x[n] (0<= x[i]<=9) is nonconstant (i.e. the tiling exists if and only if there exist such indexes i, j that x[i] is not equal to x[j]).