Chef and Food Delivery
This Bitotsav, chef has decided to open a food delivery service for the students of BIT Mesra. The map of BIT can be repersented as a weighted tree structure, where the nodes represent the hostels and the edges are the path between them which have some given weight.
The chef is now in a dilemma as to choose which hostel as his central kitchen. The choice for a particular hostel is based on the regar distance which is given as - the sum of the path from that hostel to all the other hostels. The chef is busy planning delicious dishes for the students and hence needs your help.
Help the chef by finding the regar distance for all the hostels.
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follow.
The first line of each test case contains a single integer N denoting the number of hostels.
This is followed by N-1 lines each describing a path between two hostels in the form of a, b, c denoting that there is a path between nodes a and b of length c.
For each test case, output N space seperated numbers each denoting the regar distance for each of the N hostels.
- 1 ≤ T ≤ 10
- 1 ≤ N, a, b ≤ 100000
- 1 ≤ c ≤ 100
Input: 1 6 1 2 1 1 3 2 1 4 3 4 5 4 4 6 2
Output: 18 22 26 18 34 26
For node 1:
Regar distance is given as distance to 2 + distance to 3 + distance to 4 + distance to 5 + distance to 6
This is given as 1 + 2 + 3 + 7 + 5 = 18
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS|
Fetching successful submissions