CF494D.Birthday

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Ali is Hamed's little brother and tomorrow is his birthday. Hamed wants his brother to earn his gift so he gave him a hard programming problem and told him if he can successfully solve it, he'll get him a brand new laptop. Ali is not yet a very talented programmer like Hamed and although he usually doesn't cheat but this time is an exception. It's about a brand new laptop. So he decided to secretly seek help from you. Please solve this problem for Ali.

An n-vertex weighted rooted tree is given. Vertex number 1 is a root of the tree. We define d(u, v) as the sum of edges weights on the shortest path between vertices u and v. Specifically we define d(u, u) = 0. Also let's define S(v) for each vertex v as a set containing all vertices u such that d(1, u) = d(1, v) + d(v, u). Function f(u, v) is then defined using the following formula:

The goal is to calculate f(u, v) for each of the q given pair of vertices. As the answer can be rather large it's enough to print it modulo 109 + 7.

阿里是哈米德的弟弟,明天是他的生日。哈米德希望弟弟通过自己的努力赢得生日礼物,因此给他出了一道难度较高的编程题,并告诉他:如果能成功解决,就送他一台崭新的笔记本电脑。阿里目前还远不如哥哥哈米德那样才华横溢;尽管他平时从不作弊,但这次是个例外——毕竟这关乎一台崭新的笔记本电脑!于是他决定悄悄向你求助。请帮阿里解决这道题。

给定一棵含 $ n $ 个顶点的带权有根树,其中顶点编号为 $ 1 $ 的节点为树的根。我们定义 $ d(u, v) $ 为顶点 $ u $ 与 $ v $ 之间最短路径上所有边的权值之和;特别地,定义 $ d(u, u) = 0 $。此外,对每个顶点 $ v $,定义集合 $ S(v) $ 为所有满足 $ d(1, u) = d(1, v) + d(v, u) $ 的顶点 $ u $ 所构成的集合。函数 $ f(u, v) $ 则按如下公式定义:

目标是对给定的 $ q $ 对顶点,分别计算 $ f(u, v) $。由于答案可能非常大,只需输出其对 $ 10^9 + 7 $ 取模的结果。

输入格式

In the first line of input an integer n (1 ≤ n ≤ 105), number of vertices of the tree is given.

In each of the next n - 1 lines three space-separated integers a__i, b__i, c__i (1 ≤ a__i, b__i ≤ n, 1 ≤ c__i ≤ 109) are given indicating an edge between a__i and b__i with weight equal to c__i.

In the next line an integer q (1 ≤ q ≤ 105), number of vertex pairs, is given.

In each of the next q lines two space-separated integers u__i, v__i (1 ≤ u__i, v__i ≤ n) are given meaning that you must calculate f(u__i, v__i).

It is guaranteed that the given edges form a tree.

输入的第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5),表示树的顶点数。

接下来的 n−1n-1 行中,每行包含三个用空格分隔的整数 ai, bi, cia_i,\, b_i,\, c_i(1≤ai, bi≤n1 \leq a_i,\, b_i \leq n,1≤ci≤1091 \leq c_i \leq 10^9),表示在顶点 aia_i 与 bib_i 之间存在一条权重为 cic_i 的边。

下一行包含一个整数 qq(1≤q≤1051 \leq q \leq 10^5),表示顶点对的数量。

接下来的 qq 行中,每行包含两个用空格分隔的整数 ui, viu_i,\, v_i(1≤ui, vi≤n1 \leq u_i,\, v_i \leq n),表示你需要计算 f(ui, vi)f(u_i,\, v_i)。

保证所给的边构成一棵树。

输出格式

Output q lines. In the i-th line print the value of f(u__i, v__i) modulo 109 + 7.

输出 q 行。在第 i 行输出 f(u__i, v__i) 对 109+710^9 + 7 取模的值。

输入输出样例

  • 输入#1

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

    输出#1

    10
    1000000005
    1000000002
    23
    1000000002
  • 输入#2

    8
    1 2 100
    1 3 20
    2 4 2
    2 5 1
    3 6 1
    3 7 2
    6 8 5
    6
    1 8
    2 3
    5 8
    2 6
    4 7
    6 1

    输出#2

    999968753
    49796
    999961271
    999991235
    999958569
    45130

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

首页