CF1575E.Eye-Pleasing City Park Tour

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

There is a city park represented as a tree with nn attractions as its vertices and n−1n - 1 rails as its edges. The ii-th attraction has happiness value aia_i.

Each rail has a color. It is either black if ti=0t_i = 0, or white if ti=1t_i = 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 11 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 kk tickets, meaning you can only switch train types at most kk times. In particular, you do not need a ticket to go through a path consisting of one rail color.

Define f(u,v)f(u, v) as the sum of happiness values of the attractions in the tour (u,v)(u, v), which is a simple path that starts at the uu-th attraction and ends at the vv-th attraction. Find the sum of f(u,v)f(u,v) for all valid tours (u,v)(u, v) (1≤u≤v≤n1 \leq u \leq v \leq n) that does not need more than kk tickets, modulo 109+710^9 + 7.

有一座城市公园,其结构是一棵包含 nn 个景点(作为顶点)和 n−1n - 1 条轨道(作为边)的树。第 ii 个景点的幸福值为 aia_i。

每条轨道具有某种颜色:若 ti=0t_i = 0,则为黑色;若 ti=1t_i = 1,则为白色。黑色列车仅能在黑色轨道上运行,白色列车仅能在白色轨道上运行。若你此前乘坐的是黑色列车,现在想换乘白色列车,或此前乘坐的是白色列车,现在想换乘黑色列车,则需消耗 11 张车票。

一次游览的路径必须是一条简单路径——即不能重复访问任一景点。首次登车无需消耗车票。你总共仅有 kk 张车票,意味着最多只能切换列车类型 kk 次。特别地,若整段路径仅使用一种颜色的轨道,则无需消耗任何车票。

定义 f(u,v)f(u, v) 为从第 uu 个景点出发、到第 vv 个景点结束的游览路径(即简单路径)所经过的所有景点的幸福值之和。请计算所有满足条件的游览路径 (u,v)(u, v)(其中 1≤u≤v≤n1 \leq u \leq v \leq n)对应的 f(u,v)f(u,v) 之和,并对 109+710^9 + 7 取模。

输入格式

The first line contains two integers nn and kk (2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5, 0≤k≤n−10 \leq k \leq n-1) — the number of attractions in the city park and the number of tickets you have.

The second line contains nn integers a1,a2,…,ana_1, a_2,\ldots, a_n (0≤ai≤1090 \leq a_i \leq 10^9) — the happiness value of each attraction.

The ii-th of the next n−1n - 1 lines contains three integers uiu_i, viv_i, and tit_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, 0≤ti≤10 \leq t_i \leq 1) — an edge between vertices uiu_i and viv_i with color tit_i. The given edges form a tree.

第一行包含两个整数 nn 和 kk(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5,0≤k≤n−10 \leq k \leq n-1)—— 分别表示城市公园中景点的数量以及你拥有的门票数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2,\ldots, a_n(0≤ai≤1090 \leq a_i \leq 10^9)—— 表示每个景点带来的快乐值。

接下来的 n−1n - 1 行中,第 ii 行包含三个整数 uiu_i、viv_i 和 tit_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,0≤ti≤10 \leq t_i \leq 1)—— 表示顶点 uiu_i 与 viv_i 之间的一条边,其颜色为 tit_i。所给的边构成一棵树。

输出格式

Output an integer denoting the total happiness value for all valid tours (u,v)(u, v) (1≤u≤v≤n1 \leq u \leq v \leq n), modulo 109+710^9 + 7.

输出一个整数,表示所有有效路径 (u,v)(u, v)(其中 1≤u≤v≤n1 \leq u \leq v \leq n)的总幸福值,对 109+710^9 + 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

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

首页