The first step is how to deal with
Unable to parse markup [type=CF_TEX]
.Here we can use Stirling Number of the second kind. Let's defineUnable to parse markup [type=CF_TEX]
the number of ways to putUnable to parse markup [type=CF_TEX]
different elements intoUnable to parse markup [type=CF_TEX]
sets and there are no empty set.Note that the allUnable to parse markup [type=CF_TEX]
sets are the same.For exampleUnable to parse markup [type=CF_TEX]
.The three ways areUnable to parse markup [type=CF_TEX]
.So we have:Unable to parse markup [type=CF_TEX]
How to proof it?
Unable to parse markup [type=CF_TEX]
is the number of ways to putUnable to parse markup [type=CF_TEX]
different elements intoUnable to parse markup [type=CF_TEX]
different sets and empty sets are allowed.The other side of the equation means we selectUnable to parse markup [type=CF_TEX]
sets fromUnable to parse markup [type=CF_TEX]
sets and arrange them inUnable to parse markup [type=CF_TEX]
different orders and putUnable to parse markup [type=CF_TEX]
different elements in it. So the fomula in the statement becomes:Unable to parse markup [type=CF_TEX]
Which equals to:
Unable to parse markup [type=CF_TEX]
For
Unable to parse markup [type=CF_TEX]
we can use the followingUnable to parse markup [type=CF_TEX]
DP to calculate allUnable to parse markup [type=CF_TEX]
.Unable to parse markup [type=CF_TEX]
Unable to parse markup [type=CF_TEX]
Which means we consider the
Unable to parse markup [type=CF_TEX]
sets.For
Unable to parse markup [type=CF_TEX]
we use tree DP. Let's defineUnable to parse markup [type=CF_TEX]
the number of setsUnable to parse markup [type=CF_TEX]
plus the number of some tricky situation(I'll show you what it is later) when we selectUnable to parse markup [type=CF_TEX]
edges in the subtree of nodeUnable to parse markup [type=CF_TEX]
.In addition,there should exist a nodeUnable to parse markup [type=CF_TEX]
for every selected edgeUnable to parse markup [type=CF_TEX]
thatUnable to parse markup [type=CF_TEX]
is in the subtree ofUnable to parse markup [type=CF_TEX]
.The initial value is:Unable to parse markup [type=CF_TEX]
Means we can choose node
Unable to parse markup [type=CF_TEX]
or not. When we add an childUnable to parse markup [type=CF_TEX]
ofUnable to parse markup [type=CF_TEX]
into our DP states we go through every possibleUnable to parse markup [type=CF_TEX]
and we selectUnable to parse markup [type=CF_TEX]
edges in the subtree ofUnable to parse markup [type=CF_TEX]
or we selectUnable to parse markup [type=CF_TEX]
edges in the subtree ofUnable to parse markup [type=CF_TEX]
and select edgeUnable to parse markup [type=CF_TEX]
.So we have:Unable to parse markup [type=CF_TEX]
Where
Unable to parse markup [type=CF_TEX]
is the previous state without add informations ofUnable to parse markup [type=CF_TEX]
.Note that in order to hold "there should exist a nodeUnable to parse markup [type=CF_TEX]
for every selected edgeUnable to parse markup [type=CF_TEX]
thatUnable to parse markup [type=CF_TEX]
is in the subtree ofUnable to parse markup [type=CF_TEX]
" we have to minusUnable to parse markup [type=CF_TEX]
invalid state which is "select edgeUnable to parse markup [type=CF_TEX]
but select no node in the subtree ofUnable to parse markup [type=CF_TEX]
". And for answer,we have to know what are those tricky situation.For example,we only select nodes from one subtree ofUnable to parse markup [type=CF_TEX]
and some of the edges cannot be selected according to the defination ofUnable to parse markup [type=CF_TEX]
because none of their ancestors is selected.So we have to go through all the children ofUnable to parse markup [type=CF_TEX]
and minus them.Let's defineUnable to parse markup [type=CF_TEX]
be the number of setsUnable to parse markup [type=CF_TEX]
when we selectUnable to parse markup [type=CF_TEX]
edges in the subtree of nodeUnable to parse markup [type=CF_TEX]
.So we have:Unable to parse markup [type=CF_TEX]
And now we finally have:
Unable to parse markup [type=CF_TEX]
The total complexity is
Unable to parse markup [type=CF_TEX]
. My code:https://codeforces.me/contest/1097/submission/48058882


