Let's assume that a Paf starts with some edge (v, u)
. Then, we can add to it any number of edges which are incident to vertex u. Assume that the last of them is an edge (u, w)
. Then, we can add any number of edges which are incident to vertex w. And so on.
So, for each paf there are two distinct vertices s
and t
. Let's denote the path between them as (v_1, v_2, v_3, ..., v_k)
, where s=v_1
and t=v_k
. In this Paf edges (v_1, v_2)
, (v_2, v_3)
, ... , (v_{k-1}, v_k)
will appear in this order, but we can insert more edges in between them. Let's assume that vertex v_2
has degree d
. It means, that between edges (v_1, v_2)
and (v_2, v_3)
we can insert any partial permutation of d-2
edges which are incident to v_2
.
How many such partial permutations are there? \sum_{i=0}^{d-2} \frac{(d-2)!}{i!}
, simple combinatorics. We can mark this value as f(v)
. So, for every simple path consisting of at least one edge, we need to sum the product of values of f()
for each vertex except for the endpoints, which is a straightforward tree dp.
First Solve: /boccadellaverita