This page is still under construction.

Parts of this page are still being built. What you see may change.

Cities

Interview

Time limit2sMemory limit1024 MB

Summary
Given a tree with N nodes, count the unordered pairs of cities whose unique path contains exactly K edges.
Level

Medium7 of 10

Topics
Tree, DFS, Divide and conquer, Prefix sum
Solved
No attempts yet

Problem

In a far away kingdom, there are NN cities numbered between 00 and N−1N - 1. The cities are connected by N−1N - 1 two-way roads. Each road has the same length, and connects exactly two cities, so there is a unique path between any pair of cities.

For any two cities AA and BB, let L(A,B)L(A, B) be the number of roads on the unique path between cities AA and BB. Given an integer KK, for how many pairs of cities A,BA, B is L(A,B)=KL(A, B) = K?

Input

The sample judge reads input in the following format:

  • line 11: N K
  • line 22: F[0] F[1] .. F[N - 2]
  • line 33: T[0] T[1] .. T[N - 2]

Output

The sample judge will write a single line with the return value of paths(N, K, F, T).

Constraints

  • 1≤K≤N≤100 0001 \le K \le N \le 100\,000

Examples1

  1. Example 1

    Input
    5 2
    0 0 0 3
    1 2 4 4
    
    Expected output
    4