#P8290. [省选联考 2022] 填树

    ID: 9372 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学各省省选2022Special JudgeO2优化树形 DP拉格朗日插值法

[省选联考 2022] 填树

Background

The original time limit is 2 s.

Problem Description

There is an undirected tree with nn nodes. Initially, the weight of every node is 00. KK wants to make some modifications to this tree. He will pick any node as the initial current node, and then repeat the following actions:

  1. Change the weight of the current node ii to a positive integer xx, which must satisfy lixril_i \leq x \leq r_i. Here li,ril_i, r_i are two positive integers given in the input.
  2. End the modification process, or move to a node adjacent to the current node whose weight is 00 (if no such node exists, he must end the modification process).

Now KK has two questions:

  1. After the modifications end, how many different trees can be obtained such that the difference between the maximum and minimum among all non-zero weights on the tree is at most KK? Here KK is a positive integer given in the input.
  2. What is the sum of the weights of all such trees? (The weight of a tree is defined as the sum of the weights of all nodes on the tree.)

You need to output the answers to these two questions modulo 109+710^9 + 7. We consider two trees different if and only if there exists at least one node whose weight is different.

Friendly notes:

  1. KK will modify at least one node (the initial node).
  2. In essence, KK will modify an arbitrary path in the tree, and in the end it must satisfy that the difference between the maximum and minimum weights on this path is at most KK.

Input Format

The first line contains two positive integers n,Kn, K, representing the number of nodes and the maximum allowed difference between weights.

The next nn lines each contain two positive integers li,ril_i, r_i, representing the minimum and maximum possible weight of node ii after being modified.

The next n1n - 1 lines each contain two positive integers ui,viu_i, v_i, indicating that there is an edge between node uiu_i and node viv_i. The input guarantees that these edges form a tree.

Output Format

Output two lines, each containing one integer, representing the answers to the first question and the second question modulo 109+710^9 + 7, respectively. Note that if you do not plan to answer the second question, you may output any integer on the second line. If the output file contains only one line, you may get 00 points due to an invalid format.

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

14
78

见附件中的 tree/tree2.in
见附件中的 tree/tree2.ans
见附件中的 tree/tree3.in
见附件中的 tree/tree3.ans

Hint

[Sample Explanation #1]

11 22 33 44 55 66 77 88 99 1010 1111 1212 1313 1414
Node 11 22 33 22 33 33 33 00 00
Node 22 00 33 44 00 44 33 44 55
Node 33 00 44 44 00 44 55 66

The table lists all 1414 trees that satisfy the condition. The sum of the weights of these trees is 7878.

[Constraints]

For 100%100\% of the data, 1n2001 \leq n \leq 200, 1liri1091 \leq l_i \leq r_i \leq {10}^9, 1K1091 \leq K \leq {10}^9.

Test Point nn \leq ri,Kr_i, K \leq Other Constraints
11 55 1010 None
22 3030 10910^9
33
44 500500
55 200200 200000200000
66
77 10910^9 A
88
99 None
1010

Special constraint A: all nodes form a chain, and there is an edge between the node numbered ii and the node numbered i+1i + 1.

[Scoring]

This problem has 1010 test points, each worth 1010 points. Answering the first question correctly gives 77 points, and answering the second question correctly gives 33 points.

Translated by ChatGPT 5