Editorial for Waterloo 2025 Fall A - Planting Trees
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Solution Sketch
Use linearity of expectation to reduce to a single node, i.e. "what is the expected number of trees where the node with label 1 has degree ".
Now use the fact that in the bijection between Prüfer sequences and labelled trees, if the node
has degree
in the tree, it shows up
times in the corresponding Prüfer sequence.
Counting the number of valid Prüfer sequence can be done with generating functions using NTT.
Note that you need only consider the first terms of the generating function, so you can take multiplication mod
.
Time Complexity: .
Solution 1
Let denote the set of labeled trees whose degrees belong to
.
Define the generating functions
where
are indeterminants.
Let denote the exponential generating function for
,
where
is a polynomial,
and
is the number of trees with
total vertices,
of which have degree
.
By standard generating function theory, the average number of vertices with degree
over all trees with
vertices given by
To compute this value, let denote the number of rooted labeled
trees whose degrees belong to
.
By standard generating function theory, we have
so it suffices to compute
.
We can simplify again: if we delete the root node, we are left with trees
where every node has children.
Call this class
, and let
be
its generating function.
Now we have
As
, we can apply LIFT to obtain
where
.
In particular, we need
so we can apply the generalized form of LIFT
to get the final equality.
Note that
so we get
Substituting, we obtain
Note the denominator can be easily computed via
applications
of NTT, where each iteration we discard higher order terms.
This takes
.
The tricky part is efficiently computing the numerator:
as coefficient extraction and taking partial derivatives with respect to
commute, we can apply the product rule to get
Now note that
hence
Now note that we can compute all of the coefficients of
in
applications of NTT, where each iteration we discard higher order terms.
This too takes
, and the final coefficient extraction can be
done in
.
Thus we can compute the in
.
Solution 2
We give an alternate derivation for the generating function.
Let be a random variable that is 1 if the node with label
has
degree exactly
, and 0 otherwise.
We wish to compute
for
.
Now by linearity of expectation, we have
Note that each
is identically distributed: to see this, for
,
note that the permutation
swapping the labels of
and
induces a bijection between labelled trees where the node with label
has
degree
, and labelled trees where the node with label
has degree
,
for all
.
Thus we get
so it remains to compute
.
Recall that labelled trees on vertices are in bijection with Prüfer
sequences.
In particular, if a given label
appears
times in the Prüfer sequence,
then the node with label
has degree
in the corresponding tree.
Thus we want to count the number of sequences where
the first element appears
times, and every other element appears
, or
times.
which is precisely
,
where
is as defined above.
The total number of trees is then given by summing the previous expression
over all
; this gives an
solution.
To see this is equivalent to the previous expression, the total number of trees
is
hence we get
which agrees with our previous computation.
Comments