#P6527. 「Wdoi-1」幻能采集

「Wdoi-1」幻能采集

Background

Illusory energy is a brand-new kind of energy.

Note: Click "Expand" for a better reading experience.

Problem Description

In a graph G={V,E}G=\{V,E\}, for a vertex set SVS\subset V of size CC, if there exists a vertex numbered vv such that, taking each vertex in SS as a start point and vv as the end point, one can choose CC paths that do not share multiple edges, then vv is called the "focus point" of the set SS.

The map of Gensokyo can be modeled as an edge-weighted unrooted tree with nn nodes (the length of a path is defined as the sum of the weights of all edges on the path). The sages have placed illusory energy collectors on cc nodes of the tree.

To make full use of illusory energy, the sages require that for any subset SS of these cc nodes with size at least 22 and at most a given constant kk, an energy hub that is only used to receive illusory energy transmitted by SS should be set up at every "focus point" of SS in the tree. Let one such "focus point" be vv. The cost to build this energy hub is computed as follows:

WS,v=uSd(u,v)W_{S,v}=\prod_{u \in S}d(u,v)

Here, d(u,v)d(u,v) denotes the shortest distance between the two nodes numbered uu and vv.

Since plans may change, the sages have designed multiple schemes for placing the cc illusory energy collectors, and the constant kk corresponding to each scheme is not necessarily the same.

Now, for each scheme ii, the sages want to make qiq_i queries. In each query, they ask: if we only build all the energy hubs that should be built for node xijx_{ij}, what is the total cost (the total cost equals the sum of the costs of building each energy hub)? Since Gensokyo has no computers, they have found you, who are skilled in OI\text{OI}, to help.

Of course, since the answer may be very large, you only need to output the result of the total cost mod 998244353\bmod\ 998244353.

Input Format

The first line contains two integers n,Dn,D, denoting the number of nodes and a certain parameter.

The next n1n-1 lines each contain three integers u,v,wu,v,w, denoting an undirected edge between uu and vv with length ww.

The next line contains an integer TT, denoting the number of schemes for placing illusory energy collectors.

For each scheme:

The first line contains two integers c,kc,k, denoting the number of illusory energy collectors and the upper bound on the subset size.

The second line contains cc integers, denoting the cc nodes where the collectors are placed. It is guaranteed that these cc integers are pairwise distinct.

The third line contains an integer qq, denoting the number of queries for this scheme.

The next qq lines each contain one integer xx, denoting a query for the total cost of building all hubs that should be built for node xx.

For some reason, the actual queried value is x=(x1+D×lastans)modn+1x'=(x-1+D\times lastans)\bmod n+1, where lastanslastans denotes the answer to the previous query. For the first query, lastans=0lastans=0. After changing schemes, lastanslastans is not reset to 00.

Output Format

Output several lines. Each line contains one integer, denoting the answer to the query modulo 998244353\text{998244353}.

8 0
1 2 1
1 7 1
2 3 3
2 4 1
4 5 1
4 6 2
7 8 1
1
4 2
1 3 5 6
3
1 
2
4
0
23
20
20 1
2 1 6
3 1 10
4 1 4
5 4 10
6 2 3
7 1 5
8 4 4
9 6 5
10 8 8
11 2 1
12 7 9
13 6 1
14 8 7
15 5 4
16 10 9
17 12 7
18 4 10
19 11 10
20 13 7
2
6 3
2 16 18 1 8 5 
5
19
11
18
8
20
6 3
8 3 17 13 7 20 
5
1
15
6
10
6

0
0
0
850
810
0
0
720
0
720

Hint

For 100%100\% of the testdata, 1w1091 \le w \le 10^9, 1u,v,cn1 \le u,v,c \le n, D{0,1}D\in\{0,1\}, 2kn2 \le k \le n.

Subtask ID nn max(ci,qi)max(\sum{c_i},\sum{q_i}) TT\le Special Constraints Score
11 1010 - 1010
22 10410^4 11 c=n,k100c=n,k\le 100 1515
33 10510^5 21052*10^5 k=2k=2 1010
44 D=0,k100D=0,k\le 100 1515
55 k100k \le 100 2020
66 - 3030

This problem uses bundled testing.

Translated by ChatGPT 5