CF1575E.Eye-Pleasing City Park Tour
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a city park represented as a tree with n attractions as its vertices and n−1 rails as its edges. The i -th attraction has happiness value ai .
Each rail has a color. It is either black if ti=0 , or white if ti=1 . Black trains only operate on a black rail track, and white trains only operate on a white rail track. If you are previously on a black train and want to ride a white train, or you are previously on a white train and want to ride a black train, you need to use 1 ticket.
The path of a tour must be a simple path — it must not visit an attraction more than once. You do not need a ticket the first time you board a train. You only have k tickets, meaning you can only switch train types at most k times. In particular, you do not need a ticket to go through a path consisting of one rail color.
Define f(u,v) as the sum of happiness values of the attractions in the tour (u,v) , which is a simple path that starts at the u -th attraction and ends at the v -th attraction. Find the sum of f(u,v) for all valid tours (u,v) ( 1≤u≤v≤n ) that does not need more than k tickets, modulo 109+7 .
输入格式
The first line contains two integers n and k ( 2≤n≤2⋅105 , 0≤k≤n−1 ) — the number of attractions in the city park and the number of tickets you have.
The second line contains n integers a1,a2,…,an ( 0≤ai≤109 ) — the happiness value of each attraction.
The i -th of the next n−1 lines contains three integers ui , vi , and ti ( 1≤ui,vi≤n , 0≤ti≤1 ) — an edge between vertices ui and vi with color ti . The given edges form a tree.
输出格式
Output an integer denoting the total happiness value for all valid tours (u,v) ( 1≤u≤v≤n ), modulo 109+7 .
输入输出样例
输入#1
5 0 1 3 2 6 4 1 2 1 1 4 0 3 2 1 2 5 0
输出#1
45
输入#2
3 1 1 1 1 1 2 1 3 2 0
输出#2
10