#P8821. [集训队互测 2022] 树链剖分

[集训队互测 2022] 树链剖分

Background

Please note: This problem is not a heavy-light decomposition template problem.

Problem Description

You are given a tree TT with nn nodes and mm distinct paths on the tree Ii=(ui,vi)(uivi)I_i = (u_i, v_i)(u_i\neq v_i). Specifically, IiI_i represents the set of all nodes on the simple path between uiu_i and viv_i on the tree.

Consider a path on TT, I=(u,v)I = (u, v). Define $f(I) = \sum\limits_{i = 1} ^ m\sum\limits_{j = 1} ^ m [I_i\cup I = I_j\cup I]$.

For all distinct paths II on TT, find the sum of f(I)f(I), and take the answer modulo 998244353998244353. That is, you need to compute $\left(\sum\limits_{u = 1} ^ n\sum\limits_{v = u} ^ n f((u, v))\right) \bmod 998244353$.

Input Format

The first line contains an integer SS, indicating the subtask ID. In the sample, the subtask ID is 1-1.

The second line contains two integers n,mn, m, representing the size of the tree and the number of paths.

The next n1n - 1 lines each contain two integers xi,yix_i, y_i, representing an edge of TT.

The next mm lines each contain two integers ui,viu_i, v_i, representing the ii-th path IiI_i.

It is guaranteed that all given paths are pairwise distinct.

Output Format

Output one line with one integer, the answer.

-1
3 3
1 2
2 3
1 2
2 3
1 3
32
-1
4 6
1 2
1 3
1 4
1 2
1 3
1 4
2 3
2 4
3 4
120
-1
7 7
1 2
1 3
2 4
4 5
5 6
5 7
5 7
3 1
4 7
7 1
2 6
3 6
3 5
330

Hint

This problem uses bundled testdata, with a total of 2525 subtasks, numbered 0240\sim 24. Note that the evaluated subtask ID equals the actual subtask ID +1+1.

The remainder of the subtask ID modulo 55 classifies subtasks by data size.

  • If the subtask ID modulo 55 is 00, then n,m100n, m\leq 100, denoted as A1.
  • If the subtask ID modulo 55 is 11, then n,m500n, m\leq 500, denoted as B1. Depends on A1.
  • If the subtask ID modulo 55 is 22, then n,m1557n, m\leq 1557, denoted as C1. Depends on B1.
  • If the subtask ID modulo 55 is 33, then n,m85500n, m\leq 85500, denoted as D1. Depends on C1.
  • If the subtask ID modulo 55 is 44, then n,m2×105n, m\leq 2\times 10 ^ 5, denoted as E1. Depends on D1.

The quotient of the subtask ID divided by 55 classifies subtasks by special constraints.

  • If the quotient is 00, then TT is a chain, denoted as A2.
  • If the quotient is 11, then TT is a star, denoted as B2.
  • If the quotient is 22, then all path endpoints are pairwise distinct, denoted as C2.
  • If the quotient is 33, then all paths share one common endpoint, denoted as D2.
  • If the quotient is 44, then there are no special constraints, denoted as E2. Depends on A2, B2, C2, D2.

Constraints

For 100%100\% of the data, 2n2×1052\leq n\leq 2\times 10 ^ 5, $1\leq m\leq \min(\frac {n(n - 1)} 2, 2\times 10 ^ 5)$, 1ui,vi,xi,yin1\leq u_i, v_i, x_i, y_i\leq n, and all (xi,yi)(x_i, y_i) form a tree, all Ii=(ui,vi)I_i = (u_i, v_i) are pairwise distinct, and uiviu_i\neq v_i.

The scores of each subtask are shown in the following table.

Score A1 B1 C1 D1 E1 Total
A2 11 22 33 77 2020
B2 44 1414
C2 55 77 2222
D2 33 44 55 1818
E2 22 33 99 2626
Total 66 1212 1919 3131 3232 100100

Note: Luogu evaluation has no subtask dependencies.

Source: 2022 CTT Mutual Test Round 4.

Problem setter: Alex_Wei.

Translated by ChatGPT 5