#P15076. [ICPC 2024 Chengdu R] Disrupting Communications

[ICPC 2024 Chengdu R] Disrupting Communications

题目描述

The enemy has established communication outposts across several locations, which can be represented as nodes and edges in a network. This network forms a tree --- a type of graph that is connected and has no cycles. As a specialist in communications engineering, your task is to disrupt their communications.

Each communication occurs along a simple path between two nodes in the tree. You have the ability to select a subgraph of this tree and disrupt every node in that subgraph. If the communication path includes a disrupted node, the communication is successfully disrupted. The subgraph you select must consist of a subset of nodes and edges from the original tree, and it must be connected, meaning it is also a tree.

The communication network consists of nn nodes, labeled from 11 to nn. Your mission involves answering qq separate queries. For each query, you are given two nodes, uiu_i and viv_i, and you must determine how many different subgraphs you can select to disrupt the communication between the two nodes. Since the number may be very large, you should provide the answer modulo 998244353998244353. It is possible that ui=viu_i=v_i, which indicates an internal communication within a node, and you are also able to disrupt it by selecting a subgraph that contains the node.

输入格式

The first line contains a single integer TT (1T1041\le T \le 10^4), indicating the number of test cases.

The first line of each test case contains two integers nn (2n1052\le n\le 10^5) and qq (1q1051\le q \le 10^5), denoting the number of nodes and the number of queries.

The second line contains n1n-1 integers p2,p3,,pnp_2,p_3,\ldots ,p_n (1pi<i1\le p_i < i), indicating that nodes ii and pip_i are connected by an edge.

Each of the next qq lines contains two integers ui,viu_i,v_i (1ui,vin1\le u_i,v_i \le n), representing the ii-th query.

It is guaranteed that the sum of nn and the sum of qq over all test cases do not exceed 31053\cdot 10^5, respectively.

输出格式

For each test case, output qq lines, each containing the result modulo 998244353998244353 for one of the qq queries.

2
3 2
1 1
2 3
1 2
5 3
1 1 2 2
4 5
2 4
2 3
6
5
14
13
15

提示

In the first case of the example, 66 connected subgraphs can be selected in total. For the first query, all of them can disrupt the communication. For the second query, 55 of them can disrupt the communication; the exception is the subgraph consisting only of node 33.