Separating Numbers
Practice
3.2 (33 votes)
Algorithms
Depth first search
Graphs
Problem
85% Success 4736 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

We are given a tree with $$N$$ nodes. Each node has a color $$C_i$$. We are also given $$N-1$$ 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 $$C_i = C_j$$.

Constraint:

$$ N \leq 300,000 $$

$$ C[i] \leq 100,000 $$

Input:

The first line contains a single integer $$N$$. The next $$N-1$$ 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 $$C_i$$. The last line contains $$N-1$$ space separated integers $$D_i$$- the $$i^{th}$$ integer denotes that the $$D_i^{th}$$ edge (in the order in which edges are given in input) is deleted in the $$i^{th}$$ step. 

Output:

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

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
2 votes
Tags:
GraphsAlgorithmsDepth First SearchC++
Points:30
5 votes
Tags:
AlgorithmsGraphsDynamic ProgrammingTreesNumber theoryDepth First Search
Points:30
8 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpen