Problem Set 6: Trees 1-29 Sprng 2018 A Average dstance Gven a tree, calculate the average dstance between two vertces n the tree. For example, the average dstance between two vertces n the followng tree s (d 01 + d 02 + d 03 + d 04 + d 12 + d 13 + d 14 + d 23 + d 24 + d 34 )/10 = (6 + 3 + 7 + 9 + 9 + 13 + 1 + 10 + 12 + 2)/10 = 8.6. Fgure 1: The frst sample case On the frst lne an nteger t (1 t 100): the number of test cases. Then for each test case: One lne wth an nteger n (2 n 10 000): the number of nodes n the tree. The nodes are numbered from 0 to n 1. n 1lnes,eachwththreentegersa (0 a<n), b (0 b<n)andd (1 d 1000). There s an edge between the nodes wth numbers a and b of length d. The resultng graph wll be a tree. For each testcase: One lne wth the average dstance between two vertces. ether an absolute or a relatve error of at most 10 6. Ths valueshouldhave Sample n- and 1 0 1 6 0 2 3 0 3 7 3 4 2 8.6
B. Duff n the Army tme lmt per test: 4 seconds memory lmt per test: 12 megabytes nput: standard nput : standard Recently Duff has been a solder n the army. Malek s her commander. Ther country, Andarz Gu has n ctes (numbered from 1 to n) and n - 1 bdrectonal roads. Each road connects two dfferent ctes. There exst a unque path between any two ctes. There are also m people lvng n Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of - th person s and he/she lves n cty number c. Note that there may be more than one person n a cty, also there may be no people lvng n the cty. Malek loves to order. That's why he asks Duff to answer to q queres. In each query, he gves her numbers v, u and a. To answer a query: Assume there are x people lvng n the ctes lyng on the path from cty v to cty u. Assume these people's IDs are p, p,..., p n ncreasng order. x If k = mn(x, a), then Duff should tell Malek numbers k, p, p,..., p n ths order. In the other words, Malek wants to k know a mnmums on that path (or less, f there are less than a people). Duff s very busy at the moment, so she asked you to help her and answer the queres. The frst lne of nput contans three ntegers, n, m and q (1 n, m, q 10 ). The next n - 1 lnes contan the roads. Each lne contans two ntegers v and u, endponts of a road (1 v, u n, v u). Next lne contans m ntegers c, c,..., c separated by spaces (1 c n for each 1 m). m Next q lnes contan the queres. Each of them contans three ntegers, v, u and a (1 v, u n and 1 a 10). For each query, prnt numbers k, p, p,..., p separated by spaces n one lne. k
Examples nput 4 1 3 1 4 4 2 1 4 3 4 6 1 2 10 2 3 3 3 1 1 3 2 2 3 0 3 4 Note Graph of Andarz Gu n the sample case s as follows (ID of people n each cty are wrtten next to them):
C. New Year Santa Network tme lmt per test: 2 seconds memory lmt per test: 26 megabytes nput: standard nput : standard New Year s comng n Tree World! In ths world, as the name mples, there are n ctes connected by n - 1 roads, and for any two dstnct ctes there always exsts a path between them. The ctes are numbered by ntegers from 1 to n, and the roads are numbered by ntegers from 1 to n - 1. Let's defne d(u, v) as total length of roads on the path between cty u and cty v. As an annual event, people n Tree World repars exactly one road per year. As a result, the length of one road decreases. It s already known that n the -th year, the length of the r -th road s gong to become w, whch s shorter than ts length before. Assume that the current year s year 1. Three Santas are plannng to gve presents annually to all the chldren n Tree World. In order to do that, they need some preparaton, so they are gong to choose three dstnct ctes c, c, c and make exactly one warehouse n each cty. The k-th (1 k 3) Santa wll take charge of the warehouse n cty c. It s really borng for the three Santas to keep a warehouse alone. So, they decded to buld an only-for-santa network! The cost needed to buld ths network equals to d(c, c ) + d(c, c ) + d(c, c ) dollars. Santas are too busy to fnd the best place, so they decded to choose c, c, c randomly unformly over all trples of dstnct 3 numbers from 1 to n. Santas would lke to know the expected value of the cost needed to buld the network. However, as mentoned, each year, the length of exactly one road decreases. So, the Santas want to calculate the expected after each length change. Help them to calculate the value. The frst lne contans an nteger n (3 n 10 ) the number of ctes n Tree World. Next n - 1 lnes descrbe the roads. The -th lne of them (1 n - 1) contans three space-separated ntegers a, b, l (1 a, b n, a b, 1 l 103 ), denotng that the -th road connects ctes a and b, and the length of -th road s l. The next lne contans an nteger q (1 q 10 ) the number of road length changes. Next q lnes descrbe the length changes. The j-th lne of them (1 j q) contans two space-separated ntegers r j, w (1 r n - 1, 1 w 103 j j j ). It means that n the j-th repar, the length of the rj-th road becomes w j. It s guaranteed that w s smaller than the current length of the r -th road. The same road can be repared several tmes. j q numbers. For each gven change, prnt a lne contanng the expected cost needed to buld the network n - 6 Tree World. The answer wll be consdered correct f ts absolute and relatve error doesn't exceed 10. j 3 2 3 3 1 k
Examples nput 3 2 3 1 3 3 1 4 2 2 2 1 1 1 14.0000000000 12.0000000000 8.0000000000 6.0000000000 4.0000000000 nput 6 1 3 3 2 6 1 7 1 4 4 2 3 2 1 3 4 1 2 19.6000000000 18.6000000000 16.6000000000 13.6000000000 12.6000000000 Note Consder the frst sample. There are 6 trples: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1). Because n = 3, the cost needed to buld the network s always d(1, 2) + d(2, 3) + d(3, 1) for all the trples. So, the expected cost equals to d(1, 2) + d(2, 3) + d(3, 1).
D. Book of Evl tme lmt per test: 2 seconds memory lmt per test: 26 megabytes nput: standard nput : standard Paladn Manao caught the tral of the ancent Book of Evl n a swampy area. Ths area contans n settlements numbered from 1 to n. Movng through the swamp s very dffcult, so people tramped exactly n - 1 paths. Each of these paths connects some par of settlements and s bdrectonal. Moreover, t s possble to reach any settlement from any other one by traversng one or several paths. The dstance between two settlements s the mnmum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evl has got a damage range d. Ths means that f the Book of Evl s located n some settlement, ts damage (for example, emergence of ghosts and werewolves) affects other settlements at dstance d or less from the settlement where the Book resdes. Manao has heard of m settlements affected by the Book of Evl. Ther numbers are p, p,..., p. Note that the Book m may be affectng other settlements as well, but ths has not been detected yet. Manao wants to determne whch settlements may contan the Book. Help hm wth ths dffcult task. The frst lne contans three space-separated ntegers n, m and d (1 m n 100000; 0 d n - 1). The second lne contans m dstnct space-separated ntegers p, p,..., p (1 p n). Then n - 1 lnes follow, each lne descrbes a path made n the area. A path s descrbed by a par of space-separated ntegers a and b representng the ends of ths path. m Prnt a sngle number the number of settlements that may contan the Book of Evl. It s possble that Manao receved some controversal nformaton and there s no settlement that may contan the Book. In such case, prnt 0. Examples nput 6 2 3 1 2 3 3 4 4 6 3 Note Sample 1. The damage range of the Book of Evl equals 3 and ts effects have been notced n settlements 1 and 2. Thus, t can be n settlements 3, 4 or.
Andrew plays a game called "Cvlzaton". Dma helps hm. E. Cvlzaton tme lmt per test: 1 second memory lmt per test: 26 megabytes nput: standard nput : standard The game has n ctes and m bdrectonal roads. The ctes are numbered from 1 to n. Between any par of ctes there ether s a sngle (unque) path, or there s no path at all. A path s such a sequence of dstnct ctes v 1, v 2,..., v k, that there s a road between any contguous ctes v and v + 1 (1 < k). The length of the descrbed path equals to (k - 1). We assume that two ctes le n the same regon f and only f, there s a path connectng these two ctes. Durng the game events of two types take place: 1. Andrew asks Dma about the length of the longest path n the regon where cty x les. 2. Andrew asks Dma to merge the regon where cty x les wth the regon where cty y les. If the ctes le n the same regon, then no mergng s needed. Otherwse, you need to merge the regons as follows: choose a cty from the frst regon, a cty from the second regon and connect them by a road so as to mnmze the length of the longest path n the resultng regon. If there are multple ways to do so, you are allowed to choose any of them. Dma fnds t hard to execute Andrew's queres, so he asks you to help hm. Help Dma. The frst lne contans three ntegers n, m, q (1 n 3 10 ; 0 m < n; 1 q 3 10 ) the number of ctes, the number of the roads we already have and the number of queres, correspondngly. Each of the followng m lnes contans two ntegers, a and b (a b ; 1 a, b n). These numbers represent the road between ctes a and b. There can be at most one road between two ctes. Each of the followng q lnes contans one of the two events n the followng format: 1 x. It s the request Andrew gves to Dma to fnd the length of the maxmum path n the regon that contans cty x (1 x n). 2 x y. It s the request Andrew gves to Dma to merge the regon that contans cty x and the regon that contans cty y (1 x, y n). Note, that x can be equal to y. For each event of the frst type prnt the answer on a separate lne. Examples nput 6 0 6 2 2 3 4 2 6 2 3 2 2 3 1 1 4
F. A and B and Lecture Rooms tme lmt per test: 2 seconds memory lmt per test: 26 megabytes nput: standard nput : standard A and B are preparng themselves for programmng contests. The Unversty where A and B study s a set of rooms connected by corrdors. Overall, the Unversty has n rooms connected by n - 1 corrdors so that you can get from any room to any other one by movng along the corrdors. The rooms are numbered from 1 to n. Every day А and B wrte contests n some rooms of ther unversty, and after each contest they gather together n the same room and dscuss problems. A and B want the dstance from the rooms where problems are dscussed to the rooms where contests are wrtten to be equal. The dstance between two rooms s the number of edges on the shortest path between them. As they wrte contests n new rooms every day, they asked you to help them fnd the number of possble rooms to dscuss problems for each of the followng m days. The frst lne contans nteger n (1 n 10 ) the number of rooms n the Unversty. The next n - 1 lnes descrbe the corrdors. The -th of these lnes (1 n - 1) contans two ntegers a and b (1 a, b n), showng that the -th corrdor connects rooms a and b. The next lne contans nteger m (1 m 10 ) the number of queres. Next m lnes descrbe the queres. The j-th of these lnes (1 j m) contans two ntegers x and y (1 x, y n) that means that on the j-th day A wll wrte the contest n the room x, B wll wrte n the room y. In the -th (1 m) lne prnt the number of rooms that are equdstant from the rooms where A and B wrte contest on the -th day. j j j j j j Examples nput 4 1 3 2 4 1 2 3 1 nput 4 2 3 2 4 2 1 3 0 2 Note n the frst sample there s only one room at the same dstance from rooms number 2 and 3 room number 1.