CF1260F.Colored Tree

省选/NOI-

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You're given a tree with nn vertices. The color of the ii-th vertex is hih_{i}.

The value of the tree is defined as ∑hi=hj,1≤i<j≤ndis(i,j)\sum\limits_{h_{i} = h_{j}, 1 \le i \lt j \le n}{dis(i,j)}, where dis(i,j)dis(i,j) is the number of edges on the shortest path between ii and jj.

The color of each vertex is lost, you only remember that hih_{i} can be any integer from [li,ri][l_{i}, r_{i}](inclusive). You want to calculate the sum of values of all trees meeting these conditions modulo 109+710^9 + 7 (the set of edges is fixed, but each color is unknown, so there are ∏i=1n(ri−li+1)\prod\limits_{i = 1}^{n} (r_{i} - l_{i} + 1) different trees).

给你一棵包含 nn 个顶点的树。第 ii 个顶点的颜色为 hih_{i}。

该树的值定义为 ∑hi=hj,1≤i<j≤ndis(i,j)\sum\limits_{h_{i} = h_{j}, 1 \le i \lt j \le n}{dis(i,j)},其中 dis(i,j)dis(i,j) 表示顶点 ii 与 jj 之间最短路径上的边数。

每个顶点的颜色均已丢失,你仅记得 hih_{i} 可以是区间 [li,ri][l_{i}, r_{i}](含端点)内的任意整数。你需要计算所有满足该条件的树的值之和,并对 109+710^9 + 7 取模(树的边集固定,但各顶点颜色未知,因此共有 ∏i=1n(ri−li+1)\prod\limits_{i = 1}^{n} (r_{i} - l_{i} + 1) 棵不同的树)。

输入格式

The first line contains one integer nn (2≤n≤1052 \le n \le 10^5) — the number of vertices.

Then nn lines follow, each line contains two integers lil_i and rir_i (1≤li≤ri≤1051 \le l_i \le r_i \le 10^5) denoting the range of possible colors of vertex ii.

Then n−1n - 1 lines follow, each containing two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) denoting an edge of the tree. It is guaranteed that these edges form a tree.

第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)—— 表示顶点的数量。

接下来 nn 行,每行包含两个整数 lil_i 和 rir_i(1≤li≤ri≤1051 \le l_i \le r_i \le 10^5),表示顶点 ii 可能的颜色取值范围。

然后是 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,且 u≠vu \ne v),表示树中的一条边。保证这些边构成一棵树。

输出格式

Print one integer — the sum of values of all possible trees, taken modulo 109+710^9 + 7.

输出一个整数——所有可能的树的权值之和,对 109+710^9 + 7 取模。

输入输出样例

  • 输入#1

    4
    1 1
    1 2
    1 1
    1 2
    1 2
    1 3
    3 4

    输出#1

    22

说明/提示

In the first example there are four different ways to color the tree (so, there are four different trees):

  • a tree with vertices colored as follows: {1,1,1,1}\lbrace 1,1,1,1 \rbrace. The value of this tree is dis(1,2)+dis(1,3)+dis(1,4)+dis(2,3)+dis(2,4)+dis(3,4)=10dis(1,2)+dis(1,3)+dis(1,4)+dis(2,3)+dis(2,4)+dis(3,4) = 10;
  • a tree with vertices colored as follows: {1,2,1,1}\lbrace 1,2,1,1 \rbrace. The value of this tree is dis(1,3)+dis(1,4)+dis(3,4)=4dis(1,3)+dis(1,4)+dis(3,4)=4;
  • a tree with vertices colored as follows: {1,1,1,2}\lbrace 1,1,1,2 \rbrace. The value of this tree is dis(1,2)+dis(1,3)+dis(2,3)=4dis(1,2)+dis(1,3)+dis(2,3)=4;
  • a tree with vertices colored as follows: {1,2,1,2}\lbrace 1,2,1,2 \rbrace. The value of this tree is dis(1,3)+dis(2,4)=4dis(1,3)+dis(2,4)=4.

Overall the sum of all values is 10+4+4+4=2210+4+4+4=22.

在第一个例子中,有四种不同的方式为树染色(即,存在四棵不同的树):

  • 一棵顶点染色方案为 {1,1,1,1}\lbrace 1,1,1,1 \rbrace 的树。该树的值为 dis(1,2)+dis(1,3)+dis(1,4)+dis(2,3)+dis(2,4)+dis(3,4)=10dis(1,2)+dis(1,3)+dis(1,4)+dis(2,3)+dis(2,4)+dis(3,4) = 10;
  • 一棵顶点染色方案为 {1,2,1,1}\lbrace 1,2,1,1 \rbrace 的树。该树的值为 dis(1,3)+dis(1,4)+dis(3,4)=4dis(1,3)+dis(1,4)+dis(3,4)=4;
  • 一棵顶点染色方案为 {1,1,1,2}\lbrace 1,1,1,2 \rbrace 的树。该树的值为 dis(1,2)+dis(1,3)+dis(2,3)=4dis(1,2)+dis(1,3)+dis(2,3)=4;
  • 一棵顶点染色方案为 {1,2,1,2}\lbrace 1,2,1,2 \rbrace 的树。该树的值为 dis(1,3)+dis(2,4)=4dis(1,3)+dis(2,4)=4。

所有树的值之和为 10+4+4+4=2210+4+4+4=22。

输入解题思路,AI测评打分。不知道怎么写?

首页