#P12499. 「DLESS-1」Life Lies in Movement

    ID: 11734 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>O2优化双指针 two-pointerAd-hoc洛谷比赛

「DLESS-1」Life Lies in Movement

Description

The town can be modeled as an undirected tree with nn nodes and n1n-1 edges. Each edge has a positive integer weight, and there is a household at each node. Let dis(u,v)\operatorname{dis}(u,v) denote the sum of edge weights along the unique simple path between nodes uu and vv.

The organizers will select a starting node uu and an ending node vv (it is required that uvu \neq v). The unique simple path between uu and vv will be the race course. All residents will watch the race. The household at node xx will go to the node yy on the path uvu \to v that is closest to xx (i.e., minimizes dis(x,y)\operatorname{dis}(x,y); such a node yy is unique). The distance dis(y,v)\operatorname{dis}(y,v) is defined as the "passion value" of this household, denoted by f(x,u,v)f(x, u, v).

Let g(u,v)g(u,v) be the average passion value over all households, defined as g(u,v)=1nx=1nf(x,u,v)g(u,v) = \frac{1}{n}\sum_{x=1}^n f(x,u,v). The organizers consider the race "successful" if the condition g(u,v)12dis(u,v)+kg(u,v)\ge \frac{1}{2}\operatorname{dis}(u,v)+k is met.

Given a constant kk, your task is to count the number of ordered pairs (u,v)(u, v) such that uvu \neq v and the race from uu to vv is "successful".

Formal Definition:

Given an undirected tree with nn nodes and positive edge weights. Let dis(u,v)\operatorname{dis}(u,v) denote the length of the path between uu and vv. Let yy be the node on the simple path uvu \to v that is closest to node xx. Define f(x,u,v)=dis(y,v)f(x, u, v) = \operatorname{dis}(y, v). Define g(u,v)=1nx=1nf(x,u,v)g(u,v)=\frac{1}{n}\sum_{x=1}^nf(x,u,v). Given a constant kk, count the number of ordered pairs (u,v)(u,v) such that uvu \ne v and g(u,v)12dis(u,v)+kg(u,v)\ge \frac{1}{2}\operatorname{dis}(u,v)+k.

Input Format

This problem contains multiple test cases. The first line contains a positive integer TT, the number of test cases.

For each test case: The first line contains two integers, nn and kk. The next n1n-1 lines each contain three integers x,y,vx, y, v, representing an edge connecting nodes xx and yy with weight vv. It is guaranteed that the input describes a tree.

Output Format

For each test case, output a single integer on one line, representing the answer.

4
7 3
1 6 3
6 4 1
6 7 4
2 6 2
3 6 1
5 6 2
6 1
2 5 4
2 1 1
2 3 3
2 4 2
6 2 2
10 2
3 2 3
4 9 2
3 10 4
10 4 1
7 6 1
3 5 3
9 8 2
7 10 1
8 1 2
10 1
1 7 2
3 2 3
8 6 4
5 4 2
9 3 2
4 10 3
10 1 4
2 5 3
9 6 2

0
3
2
24

Hint

【Data Range】

For all test cases, it is guaranteed that:

  • 1T1041\le T\le 10^4
  • 1n,n1061\le n,\sum n\le 10^6.
  • 1v,k1061\le v,k\le10^6.

This problem uses subtasks for scoring. The descriptions of the subtasks are as follows:

Subtask n\sum n\le Score
11 500500 55
22 20002000 1515
33 50005000 2020
44 10510^5
55 3×1053\times10^5
66 10610^6