strict
A binary tree
is considered strict if all its internal nodes have exactly two children. Task Input Data Output Data Constraints Example 5 7 A solution can be
represented by trees 2, 3 and 5 Time limit: 0.1 seconds/test Iolanda Popa
A set of n strict binary trees
is given, each tree having m nodes
numbered from 1 to m.
We recurrently define the equality between two trees as having fixed roots in
nodes x, and respectively y.
A(x) = A(y) if:
or
(where we noted as A(x) the tree
with a root x)
In the same way we can define the "<" order relationship
A(x) < A(y) if:
or
or
Determine the length of the longest increasing subsequence (not necessarily
strict) of trees from the given set, considering the order relationship defined
above.
Input file strict.in has the following structure:
- line one contains two positive integers separated by a space n
m, representing the number of trees and, respectively, the size of the
trees
- each of the following n lines
contains m - 1 + (m + 1) / 2
numbers; line i + 1 contains
required information for tree i
as follows: for every node from 1
to m it contains a 0
if the node has no children or two numbers representing the left and, respectively,
the right child; first node 1 is described, then node 2, etc. Line: 0
0 5 1 0 0 7 3 2 4 means that nodes 1 and 2 have no children,
node 3 has as a left child node 5 and as a right child node 1, node 4 has no
children,
and so on.
Output file strict.out
will contain a single number representing the length of the longest
increasing subsequence.
0 < n <= 10000
0 < m <= 51
Champions can realize
why in a strict binary tree there is only one node which can fulfill the role
of tree root. Due to this, the relationship is well defined.
40% of the tests will have n
<= 5000 and m <=
31.
Participants are encouraged to use functions like fgets()
or fread() to read data.
Be there x=(x1, x2,
..., xn) a sequence of n
elements. A subsequence of x
is comprised of elements from x,
in the order in which they appear in x
(xi1, xi2,
..., xik with i1<i2<...<ik).
strict.in
strict.out
Explanation
0 0 5 1 0 0 7 3 2 4
3 7 6 4 0 1 5 0 0 0
7 5 0 0 0 6 3 0 2 4
4 3 0 0 5 6 0 0 2 1
2 5 3 4 0 7 6 0 0 0
3
student - Faculty of Informatics, Iasi
Contact: iolivp@gmail.com