#P8971. 『GROI-R1』 虹色的彼岸花

『GROI-R1』 虹色的彼岸花

Background

A boy wears a dark gray spring school uniform jacket and black shorts, with a clean white shirt inside.

For some reason, his right hand is wrapped in bandages, and his right eye is tightly covered by his hair, giving off a mysterious feeling.

A bright red higanbana petal spins above the classroom, where no one pays attention.

Qi’s background is always a mystery.

"So who are you, and why did you come here!"

But the higanbana clearly does not want you to know any of that.

Problem Description

Qi gave Han a tree numbered 1n1\sim n. Each node originally had a node value, and some edges have edge weights while others do not. However, Qi erased the node values. Han only knows that all node values are integers, and are between ll and rr (inclusive). Also, node values and edge weights satisfy the following special relationship:

  • For edges with edge weights, the sum of the node values of the two endpoints must equal the edge weight.

  • For edges without edge weights, there is no restriction.

Qi asks Han how many different ways there are to fill in the node values. Two fillings are different if and only if there exists at least one node whose value is different. However, Han cannot solve this problem.

Han asks you to solve it.

Input Format

This problem has multiple test cases.

The first line contains an integer TT, representing the number of test cases.

For each test case:

The first line contains three integers n,l,rn,l,r, meaning the tree has nn nodes, and the node value range is [l,r][l,r].

The next n1n-1 lines each start with an integer opop. If op=0op=0, this edge has no edge weight; if op=1op=1, this edge has an edge weight.

  • If op=0op=0, then input two integers u,vu,v, meaning this edge connects nodes u,vu,v.

  • If op=1op=1, then input three integers u,v,wu,v,w, meaning there is an edge of weight ww connecting nodes u,vu,v.

Output Format

For each test case, output one integer per line, representing the number of ways to fill in the node values. The answer should be taken modulo 109+710^9+7.

2
6 0 4
1 1 2 2
1 2 3 4
1 3 4 2
0 2 5
1 4 6 3

6 -1 4
1 1 2 4
0 2 3
0 3 4
0 2 5
0 4 6
5
6480

Hint

Sample Explanation

For the first test case in the sample, we can get the following graph:

The 55 filling methods are:

$\{0,2,2,0,0,3\}\\\{0,2,2,0,1,3\}\\\{0,2,2,0,2,3\}\\\{0,2,2,0,3,3\}\\\{0,2,2,0,4,3\}$

It can be proven that no other filling methods exist.

In the sample input, blank lines were added for readability. In the actual testdata, there are no extra blank lines.

Constraints

This problem uses bundled tests.

Subtask ID Constraints Special Property Score
Subtask1\text{Subtask1} 1n101\le n\le 100l,r50\le l,r\le5 1515
Subtask2\text{Subtask2} 1n2×1051\le n\le 2\times 10^50l,r50\le l,r\le5 2020
Subtask3\text{Subtask3} 1n101\le n\le 10109l,r109-10^9\le l,r\le 10^9 1515
Subtask4\text{Subtask4} 1n2×1051\le n\le 2\times10^5109l,r109-10^9\le l,r\le 10^9 Yes 1010
Subtask5\text{Subtask5} 4040

Special Property: It is guaranteed that every edge has no edge weight.

For 100%100\% of the data, it is guaranteed that 1T51\le T \le 5, 1n2×1051\le n\le 2\times10^5, 1n1061\le \sum n\le 10^6, 109lr109-10^9\le l\le r \le 10^9, 109w109-10^9\le w\le 10^9, op{0,1}op\in\{0,1\}.

Translated by ChatGPT 5