A network consisting of M cities and M-1 roads connecting them is given. Cities are labeled with distinct integers within the range [o. (M-1)] Roads connect cities in such a way that each pair of distinct cities is connected either by a direct road or along a path consisting of direct roads. There is exactly one way to reach any city from any other city. In other words, cities and direct roads form a tree. The number of direct roads that must be traversed is called the distance between these two cities. For example, consider the following network consisting of ten cities and nine roads: 2 0 Cities 2 and 4 are connected directly, so the distance between them is 1. Cities 4 and 7 are connected by a path consisting of the direct roads 4-0,0-9 and 9-7; hence the distance between them is 3. One of the cities is the capital, and the goal is to count the number of cities positioned away from it at each of the distances 1,2,3,.., M -1. If city number 1 is the capital, then the cities positioned at the various distances from the If city number 1 is the capital, then the cities positioned at the various distances from the capital would be as follows: . 9 is at a distance of 1 · 0, 3, 7 are at a distance of 2; 8,4 are at a distance of 3; 2, 5, 6 are at a distance of 4. Write a function: class Solution t public int[] solution(int[] T)h that, given a non-empty array T consisting of M integers describing a network of M cities and M 1 roads, returns an array consisting of M-1 integers, specifying the number of cities positioned at each distance 1, 2,…, M – 1. Array T describes a network of cities as follows: · if T[P] Q and P = Q, then P is the capital; if T[P Q and P Q, then there is a direct road between cities P and Q. For example, given the following array T consisting of ten elements: T[2] 4 T[6]8 T[9] = 1 = 9 T[7] the function should return [1, 3, 2,3,0,0,0,0,01, as explained above. Write an efficient algorithm for the following assumptions: M is an integer within the range [1..100,000]; each element of array T is an integer within the range [0.M-1] there is exactly one (possibly indirect) connection between any two distinct cities.
Why Work with Us
Top Quality and Well-Researched Papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional and Experienced Academic Writers
We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.
Free Unlimited Revisions
If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.
Prompt Delivery and 100% Money-Back-Guarantee
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & Confidential
We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.
Admissions
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Reviews
Editing Support
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
Reviews
Revision Support
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.