9600: Christmas Tree

内存限制:512 MB 时间限制:4 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

There are nnn light bulbs on the Christmas tree, connected by (n−1)(n - 1)(n1) wires. Each wire connects two different bulbs, forming a tree rooted at the 111-st bulb. Initially, the beauty of the Christmas tree is 000, and you can light up some bulbs to increase the beauty.

Lighting up the iii-th bulb consumes aia_iai units of power and increases the beauty by bib_ibi. Due to the heat generated by the wire, for the jjj-th wire, if both the uju_juj-th bulb and the vjv_jvj-th bulb it connects are lit, an additional cjc_jcj units of power will be consumed. Since lit bulbs also generate heat, for safety reasons, each lit bulb must be directly connected by a wire to at least one unlit bulb.

There is a power meter with a range of mmm, which shows the total power consumption in modulo mmm. There are qqq queries, each providing rrr and kkk, and you need to find the maximum beauty of the Christmas tree when only lighting up bulbs in the subtree rooted at the rrr-th bulb, keeping bulbs outside the subtree unlit, and the power meter shows kkk, as well as the number of ways to light up the bulbs that can achieve the maximum beauty.

Two ways to light up the bulbs are considered different if and only if there exists at least one bulb that is lit in one way but not in the other. Since the number of ways may be large, you just need to output it modulo (109+7). If there is no feasible way, the maximum beauty is considered to be −1-11, and the number of ways is 000.

输入格式

The first line contains three integers nnn, mmm and qqq (1≤n≤1001 \leq n \leq 1001n100, 1<=m<=3*104,1<=q<=105), indicating the number of bulbs on the Christmas tree, the range of the power meter, and the number of queries.
Then nnn lines follow, the iii-th of which contains two integers aia_iai and bib_ibi (1<=ai,bi<=106), indicating that lighting up the iii-th bulb consumes aia_iai units of power and increases the beauty by bib_ibi.
Then (n−1)(n - 1)(n1) lines follow, the jjj-th of which contains three integers uju_juj, vjv_jvj and cjc_jcj (1≤ui,vj≤n1 \leq u_i, v_j \leq n1ui,vjn, 1<=cj<=106), indicating that the jjj-th wire connects the uju_juj-th bulb and the vjv_jvj-th bulb, and if these bulbs are both lit, an additional cjc_jcj units of power will be consumed. It is guaranteed that the input wires connect all bulbs forming a tree.
Then qqq lines follow, each of which contains two integers rrr and kkk (1≤r≤n1 \leq r \leq n1rn, 0≤k<m0 \leq k < m0k<m), indicating a query.

输出格式

Output qqq lines, each of which contains two integers, indicating the maximum beauty of the Christmas tree when only lighting up bulbs in the subtree rooted at the rrr-th bulb, keeping bulbs outside the subtree unlit, and the power meter shows kkk, as well as the number of ways to light up the bulbs that achieve the maximum beauty, in modulo (109+7). If there is no feasible way, the maximum beauty is considered to be −1-11, and the number of ways is 000.

输入样例 复制

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

输出样例 复制

4 1
5 2
2 1
7 1

数据范围与提示

In the second sample case, a single bulb cannot be lit.