Problem Set 6: Trees Spring 2018

Similar documents
Problem Set 9 Solutions

Complex Numbers Alpha, Round 1 Test #123

Calculation of time complexity (3%)

Affine transformations and convexity

Math1110 (Spring 2009) Prelim 3 - Solutions

Complete subgraphs in multipartite graphs

Société de Calcul Mathématique SA

Chapter 3 Describing Data Using Numerical Measures

Min Cut, Fast Cut, Polynomial Identities

Dynamic Programming. Preview. Dynamic Programming. Dynamic Programming. Dynamic Programming (Example: Fibonacci Sequence)

Problem Do any of the following determine homomorphisms from GL n (C) to GL n (C)?

Finding Dense Subgraphs in G(n, 1/2)

HMMT February 2016 February 20, 2016

find (x): given element x, return the canonical element of the set containing x;

Lecture 4: Universal Hash Functions/Streaming Cont d

8.6 The Complex Number System

College of Computer & Information Science Fall 2009 Northeastern University 20 October 2009

Kernel Methods and SVMs Extension

A 2D Bounded Linear Program (H,c) 2D Linear Programming

Lecture 3: Probability Distributions

Expected Value and Variance

CS 331 DESIGN AND ANALYSIS OF ALGORITHMS DYNAMIC PROGRAMMING. Dr. Daisy Tang

NP-Completeness : Proofs

Turing Machines (intro)

Simultaneous Optimization of Berth Allocation, Quay Crane Assignment and Quay Crane Scheduling Problems in Container Terminals

Graph Reconstruction by Permutations

3.1 Expectation of Functions of Several Random Variables. )' be a k-dimensional discrete or continuous random vector, with joint PMF p (, E X E X1 E X

MTH 263 Practice Test #1 Spring 1999

Lectures - Week 4 Matrix norms, Conditioning, Vector Spaces, Linear Independence, Spanning sets and Basis, Null space and Range of a Matrix

On quasiperfect numbers

On the Multicriteria Integer Network Flow Problem

Exercises. 18 Algorithms

Caps and Colouring Steiner Triple Systems

EEL 6266 Power System Operation and Control. Chapter 3 Economic Dispatch Using Dynamic Programming

n α j x j = 0 j=1 has a nontrivial solution. Here A is the n k matrix whose jth column is the vector for all t j=0

= z 20 z n. (k 20) + 4 z k = 4

Volume 18 Figure 1. Notation 1. Notation 2. Observation 1. Remark 1. Remark 2. Remark 3. Remark 4. Remark 5. Remark 6. Theorem A [2]. Theorem B [2].

Problem Solving in Math (Math 43900) Fall 2013

Week3, Chapter 4. Position and Displacement. Motion in Two Dimensions. Instantaneous Velocity. Average Velocity

Linear Regression Analysis: Terminology and Notation

Chapter 5. Solution of System of Linear Equations. Module No. 6. Solution of Inconsistent and Ill Conditioned Systems

Module 9. Lecture 6. Duality in Assignment Problems

Introduction to Vapor/Liquid Equilibrium, part 2. Raoult s Law:

2E Pattern Recognition Solutions to Introduction to Pattern Recognition, Chapter 2: Bayesian pattern classification

U.C. Berkeley CS294: Beyond Worst-Case Analysis Handout 6 Luca Trevisan September 12, 2017

Lecture 5 Decoding Binary BCH Codes

Generalized Linear Methods

APPENDIX 2 FITTING A STRAIGHT LINE TO OBSERVATIONS

U.C. Berkeley CS294: Beyond Worst-Case Analysis Luca Trevisan September 5, 2017

ACTM State Calculus Competition Saturday April 30, 2011

Difference Equations

6. Stochastic processes (2)

a. (All your answers should be in the letter!

6. Stochastic processes (2)

Multilayer Perceptron (MLP)

Chapter 8. Potential Energy and Conservation of Energy

Exercises of Chapter 2

PhysicsAndMathsTutor.com

COMPLEX NUMBERS AND QUADRATIC EQUATIONS

Introduction to Information Theory, Data Compression,

763622S ADVANCED QUANTUM MECHANICS Solution Set 1 Spring c n a n. c n 2 = 1.

20. Mon, Oct. 13 What we have done so far corresponds roughly to Chapters 2 & 3 of Lee. Now we turn to Chapter 4. The first idea is connectedness.

NUMERICAL DIFFERENTIATION

First Year Examination Department of Statistics, University of Florida

Homework 9 Solutions. 1. (Exercises from the book, 6 th edition, 6.6, 1-3.) Determine the number of distinct orderings of the letters given:

Maximizing the number of nonnegative subsets

8.1 Arc Length. What is the length of a curve? How can we approximate it? We could do it following the pattern we ve used before

STAT 511 FINAL EXAM NAME Spring 2001

THE CHINESE REMAINDER THEOREM. We should thank the Chinese for their wonderful remainder theorem. Glenn Stevens

Chapter 8 SCALAR QUANTIZATION

On cyclic of Steiner system (v); V=2,3,5,7,11,13

More metrics on cartesian products

Foundations of Arithmetic

Statistics MINITAB - Lab 2

Analytical Chemistry Calibration Curve Handout

Case A. P k = Ni ( 2L i k 1 ) + (# big cells) 10d 2 P k.

Edge Isoperimetric Inequalities

Subset Topological Spaces and Kakutani s Theorem

LECTURE V. 1. More on the Chinese Remainder Theorem We begin by recalling this theorem, proven in the preceeding lecture.

Structure and Drive Paul A. Jensen Copyright July 20, 2003

arxiv: v2 [cs.ds] 1 Feb 2017

Ph 219a/CS 219a. Exercises Due: Wednesday 23 October 2013

PHYSICS - CLUTCH CH 28: INDUCTION AND INDUCTANCE.

Module 2. Random Processes. Version 2 ECE IIT, Kharagpur

2.3 Nilpotent endomorphisms

Please initial the statement below to show that you have read it

Learning Theory: Lecture Notes

Section 8.1 Exercises

Lecture 4: November 17, Part 1 Single Buffer Management

Stanford University CS359G: Graph Partitioning and Expanders Handout 4 Luca Trevisan January 13, 2011

CHAPTER 13. Exercises. E13.1 The emitter current is given by the Shockley equation:

Lecture Notes 7: The Unruh Effect

Errors for Linear Systems

Prof. Dr. I. Nasser Phys 630, T Aug-15 One_dimensional_Ising_Model

a b a In case b 0, a being divisible by b is the same as to say that

The path of ants Dragos Crisan, Andrei Petridean, 11 th grade. Colegiul National "Emil Racovita", Cluj-Napoca

Spectral Graph Theory and its Applications September 16, Lecture 5

Self-complementing permutations of k-uniform hypergraphs

DEMO #8 - GAUSSIAN ELIMINATION USING MATHEMATICA. 1. Matrices in Mathematica

PHYS 705: Classical Mechanics. Newtonian Mechanics

Transcription:

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.