Decomposition of paths

1.9

8 votes
Data Structures, Dynamic Programming, Trees, Lowest Common Ancestor
Problem

You are given a tree that contains n vertices and q queries. Each query is determining the number of decompositions of the tree into paths and the path from v to u that is one of the paths in the decomposition modulo 998244353.

A tree decomposition in paths is valid if each vertex belongs to exactly one path. A path can be a single vertex. Two decompositions are different if there are two vertices v and u that belong to the same path in one of the decompositions but two distinct paths in the other.

Input format

  • The first line contains n denoting the number of vertices in the tree.
  • Each of following n1 lines contains space-separated integers vi,ui describing the tree's ith edge's vertex (vi,ui).
  • Each of following q lines contains space-separated integers v,u describing the ith query's path that is from v to u (v and u can be equal).

Output format

For each query, print the answer of a specific query in a line.

Constraints

1n,q105

1vi,uin

It is guaranteed that the edges form a tree.

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

In the first query, the tree else path from 3 to 3 is a path with 3 edges so it has 8 path decompositions.
And in the Second query, the tree else path from 2 to 5 is a path with 2 edges so it has 4 path decompositions.

Editor Image

?