310600: CF1857G. Counting Graphs

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

G. Counting Graphstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Given a tree consisting of $n$ vertices. A tree is a connected undirected graph without cycles. Each edge of the tree has its weight, $w_i$.

Your task is to count the number of different graphs that satisfy all four conditions:

  1. The graph does not have self-loops and multiple edges.
  2. The weights on the edges of the graph are integers and do not exceed $S$.
  3. The graph has exactly one minimum spanning tree.
  4. The minimum spanning tree of the graph is the given tree.

Two graphs are considered different if their sets of edges are different, taking into account the weights of the edges.

The answer can be large, output it modulo $998244353$.

Input

The first line contains an integer $t$ ($1\le t\le 10^4$) — the number of test cases.

The first line of each test case contains two integers $n$ and $S$ ($2 \le n \le 2 \cdot 10^5, 1\le S\le 10^9$) — the number of vertices and the upper bound of the weights.

Then follow $n-1$ lines describing the tree, the $i$-th line contains three integers $u_i$, $v_i$, and $w_i$ ($1\le u_i,v_i\le n, u_i \ne v_i, 1\le w_i\le S$) — an edge in the tree with weight $w_i$.

It is guaranteed that the sum of $n$ for all tests does not exceed $2\cdot 10^5$.

Output

For each test, output the number of different graphs that satisfy the conditions, modulo $998244353$.

ExampleInput
4
2 5
1 2 4
4 5
1 2 2
2 3 4
3 4 3
5 6
1 2 3
1 3 2
3 4 6
3 5 1
10 200
1 2 3
2 3 33
3 4 200
1 5 132
5 6 1
5 7 29
7 8 187
7 9 20
7 10 4
Output
1
8
80
650867886
Note

In the first sample, there is only one graph, which is the given tree.

In the second samle, the given tree looks like this:

All possible graphs for the second sample are shown below, the minimum spanning tree is highlighted in red:

Output

题目大意:计算满足以下条件的不同图的数量:

1. 图没有自环和多边。
2. 图的边权重为整数且不超过S。
3. 图恰好有一个最小生成树。
4. 图的最小生成树是给定的树。

两个图如果边的集合(考虑边的权重)不同,则被认为是不同的。

答案可能很大,输出对998244353取模的结果。

输入输出数据格式:

输入:
- 第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。
- 每个测试用例的第一行包含两个整数n和S(2≤n≤2×10^5, 1≤S≤10^9)——顶点的数量和权重的上限。
- 接下来n-1行描述树,第i行包含三个整数u_i, v_i, 和 w_i(1≤u_i,v_i≤n, u_i ≠ v_i, 1≤w_i≤S)——树中的一条边,权重为w_i。
- 保证所有测试的n之和不超过2×10^5。

输出:
- 对于每个测试用例,输出满足条件的不同图的数量,对998244353取模的结果。题目大意:计算满足以下条件的不同图的数量: 1. 图没有自环和多边。 2. 图的边权重为整数且不超过S。 3. 图恰好有一个最小生成树。 4. 图的最小生成树是给定的树。 两个图如果边的集合(考虑边的权重)不同,则被认为是不同的。 答案可能很大,输出对998244353取模的结果。 输入输出数据格式: 输入: - 第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。 - 每个测试用例的第一行包含两个整数n和S(2≤n≤2×10^5, 1≤S≤10^9)——顶点的数量和权重的上限。 - 接下来n-1行描述树,第i行包含三个整数u_i, v_i, 和 w_i(1≤u_i,v_i≤n, u_i ≠ v_i, 1≤w_i≤S)——树中的一条边,权重为w_i。 - 保证所有测试的n之和不超过2×10^5。 输出: - 对于每个测试用例,输出满足条件的不同图的数量,对998244353取模的结果。

加入题单

上一题 下一题 算法标签: