#P8047. [COCI 2015/2016 #4] GALAKSIJA

[COCI 2015/2016 #4] GALAKSIJA

Problem Description

A long time ago, in a galaxy far, far away, there were nn planets and n1n-1 edges connecting all planets. In other words, the planets and edges formed a tree. Also, each edge had a weight.

A planet pair (A,B)(A,B) is boring if and only if the following conditions hold:

  • ABA \neq B.
  • Planets AA and BB can reach each other.
  • The xor sum of the weights of all edges on the path between AA and BB equals 00.

Now, due to an uncontrollable force, all edges in the galaxy will be destroyed in a fixed order. Please output the number of boring planet pairs at the beginning and after each time an edge is destroyed.

Input Format

The first line contains an integer nn, the number of planets.
Then follow n1n-1 lines, each containing three integers ai,bi,zia_i, b_i, z_i, meaning that the ii-th edge connects nodes aia_i and bib_i, and its weight is ziz_i.
Then a line contains n1n-1 integers; the ii-th integer pip_i indicates the index of the edge destroyed in the ii-th destruction.

Output Format

Output nn lines, each containing one integer. The ii-th integer denotes the number of boring planet pairs after destroying i1i-1 edges in order.

2
1 2 0
1
1
0
3
1 2 4
2 3 4
1 2
1
0
0
4
1 2 0
2 3 0
2 4 0
3 1 2
6
3
1
0

Hint

[Sample 1 Explanation]

Initially, only the planet pair (1,2)(1,2) is boring. After destroying the only edge, there are no boring planet pairs anymore.

[Sample 2 Explanation]

Initially, only the planet pair (1,3)(1,3) is boring. After destroying edge 11, since planets 11 and 33 can no longer reach each other, there are no boring planet pairs afterward.

[Sample 3 Explanation]

Initially, all planet pairs are boring, because for every pair of planets, the xor sum of the weights of all edges on the path between them is 00.

[Constraints]

For all testdata, 1n1051 \leqslant n \leqslant 10^5, 1ai,bin1 \leqslant a_i, b_i \leqslant n, 0zi1090 \leqslant z_i \leqslant 10^9.

There are 1010 test points in total. The constraints for each test point are as follows:

Test Point nn \leqslant Special Property A
11 10001000 \surd
22 ×\times
343 \sim 4 10510^5 \surd
5105 \sim 10 ×\times

Special Property A: The weight of every edge is 00. In other words, i[1,n)\forall i \in [1, n), zi=0z_i = 0.

[Source]

This problem comes from COCI 2015-2016 CONTEST 4 T5 GALAKSIJA. With the original testdata configuration, the full score is 140140 points.

Translated and organized by Eason_AC.

Translated by ChatGPT 5