Boring Planet Pairs

Count the planet pairs whose path xor is zero before any deletion and after each edge removal in order.

Medium7Union-findHash mapNo attempts yetTime limit1sMemory limit64 MB

Problem

A long time ago in a galaxy far, far away there were NN planets. There were also N1N-1 interplanetary paths, and those paths connected every planet directly or indirectly. In other words, the planets and the paths form a tree. Each path carries one integer, the curiosity of that path.

A pair of planets AA, BB is boring when all three conditions hold.

  • AA and BB are different planets.
  • You can travel from AA to BB along one or more interplanetary paths.
  • The binary XOR of the curiosity of every path on that travel is 00.

The pair (A,B)(A, B) and the pair (B,A)(B, A) count as the same pair.

Times have changed and an evil emperor now rules the galaxy. He uses the Force to destroy all the interplanetary paths in a fixed order. Count the boring pairs before the destruction starts and after each destruction.

Input

The first line contains the number of planets NN. (1N1000001 \le N \le 100\,000)

Each of the next N1N-1 lines contains three integers AiA_i, BiB_i, ZiZ_i separated by spaces, which mean that planet AiA_i and planet BiB_i are directly connected by a path of curiosity ZiZ_i. (1Ai,BiN1 \le A_i, B_i \le N, 0Zi10000000000 \le Z_i \le 1\,000\,000\,000)

The last line contains a permutation of the integers from 11 to N1N-1. If the ii-th value of the permutation is jj, the emperor destroys the jj-th path of the input at step ii. When N=1N = 1, this line contains no numbers.

Output

Print NN lines. The kk-th line holds the number of boring pairs left after exactly k1k-1 paths are destroyed.

Hint

In the first example the pair of planets 11 and 22 is boring before the destruction. Once the path is destroyed, no route connects them.

In the second example the pair (1,3)(1, 3) is boring before the destruction. After the first destruction and after the second one, travel between planet 11 and planet 33 is impossible, and none of the remaining pairs is boring.

In the third example every path has curiosity 00, so every pair of planets that still has a route between them is boring.