LCC '21 Contest 2 S5 - Pretty Places

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 2.5s
Memory limit: 256M

Author:
Problem type

Ahano loves pretty places and pretty paths.

Her city has N places, each with a prettiness p_i. The places are connected by a network of N-1 bidirectional roads, in which any two places are connected by some sequence of roads.

A path from place a to place b is defined as the sequence of places visited on the traversal from a to b, where a < b. The average prettiness of a path is defined as the numerical median of the p_i in the path. For example, if the path from a to b visits places with the following p_i: [3, 2, 8, 4, 6], then the average prettiness of the path is the numerical median, 4.

Ahano likes a path if the number of places on the path is odd and the average prettiness of the path is greater than or equal to K. How many paths does Ahano like?

Input Specification

The first line of input contains two space-separated integers, N (1 \le N \le 2\times10^{5}) and K (0 \le K \le 10^{5}).

The next line contains N space separated integers, p_i (0 \le p_i \le 10^{5}).

Each of the next N-1 lines contains two space-separated integers, representing a bidirectional road between two places.

Output Specification

Output a single integer, the number of paths Ahano likes.

Subtasks

Subtask 1 [40%]

(1 \le N \le 10^{3})

Subtask 2 [60%]

(1 \le N \le 2\times10^{5})

Sample Input 1

5 3
6 2 8 1 2
1 2
2 3
2 4
1 5

Sample Output 1

1

Explanation

The path from 1 to 3 consists of the prettiness values [6, 2, 8]. Ahano likes the path since it contains an odd number of places, and the numerical median is 6 which is greater than K.

Sample Input 2

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

Sample Output 2

2

Explanation

The path from 3 to 5 consists of the prettiness values [1, 6, 4]. Ahano likes the path since it contains an odd number of places, and the numerical median is 4 which is greater than K.

The path from 1 to 5 consists of the prettiness values [3, 2, 1, 6, 4]. Ahano likes the path since it contains an odd number of places, and the numerical median is 3 which is equal to K.


Comments

There are no comments at the moment.