Separating Numbers

3.2

33 votes
Algorithms, Depth First Search, Graphs
Problem

We are given a tree with N nodes. Each node has a color Ci. We are also given N1 queries and in each query we are told to destroy a previously undestroyed edge. Every time we destroy an edge, we have to report the number of pairs of nodes that get disconnected. Here, two nodes i and j are said to be disconnected  if before the destruction you could reach from i to j using edges not yet destroyed , and if Ci=Cj.

Constraint:

N300,000

C[i]100,000

Input:

The first line contains a single integer N. The next N1 lines contain two space separated integers a and b which means that there is an edge between a and b in the tree. The next line contains N space separated integers where the i integer denotes the value of Ci. The last line contains N1 space separated integers Di- the ith integer denotes that the Dthi edge (in the order in which edges are given in input) is deleted in the ith step. 

Output:

For every deleted edge, output the number of disconnected values on a new line. 

 

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

<insert later>

Editor Image

?