Grain
Time limit3sMemory limit1024 MB
After each prince's castle is built, print twice the sum of road distances over all pairs of castles, counting village 1 as a castle.
- Level
Medium7 of 10
- Topics
- Tree, Segment tree, DFS
- Solved
- No attempts yet
Problem
Bajtocja is a beautiful land, fairly ruled by King Bajtazar 222nd. It has villages numbered from 1 to . The villages are connected by roads that form a connected network. The king's castle stands in village 1.
The king has sons who will soon come of age. Each adult prince needs a castle of his own, so new castles will be built in some villages.
The king and his sons must communicate about matters such as the safety of Bajtocja. For this, messengers are sent every day from each castle to every other castle. The horses that carry the messengers must eat grain before each trip, and each horse eats 1 decagram of grain per kilometer traveled.
Two castles need two messengers to communicate fully: one leaves the first castle for the second, and the other leaves in the opposite direction. Write a program that computes Bajtocja's daily grain demand after each new castle is built.
Input
The first line contains two integers and (), the number of villages and the number of princes.
Each of the next lines contains three integers , , and (, ), describing a road of length kilometers between villages and .
Each of the next lines contains one integer (), the village where the next prince builds his castle. No village gets more than one castle, so all values of are pairwise distinct.
Output
Print lines. The -th line contains the amount of grain (in decagrams) that the messengers need after the castle of the -th prince is built.