Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In the distant Algorithms Galaxy, there's a solar system with K planets that inhabit intelligent alien beings! All planets are the same distance from the sun, so we can imagine they form a ring around it.
- Aliens have designed a hierarchical transport network that allows them to move between planets. In particular, in the largest country on each planet, there is a spacecraft used for space flights to both adjacent planets. So if we number the planets from 0 to K-1, the spacecraft on planet i is used for flights to and from the planets (i - 1) mod K and (i + 1) mod K.
- To move between countries on the same planet, aliens have designed road networks corresponding to binary trees (the nodes of the tree correspond to the countries on the planet and the edges of the tree correspond to the roads). The tree root is the largest country on the planet, the one where the site is located (NB: The binary tree corresponding to the road network of each planet is not necessarily complete. Thus, each country (hub) can have one, one or two countries (children) with which it is connected by road.)
- A wealthy resident of this solar system wants to make a long journey to admire the attractions of different countries. However, he does not want to go through the same country twice, as he considers it a waste of time. So, his route will be given by a simple path that starts and ends in different countries. Therefore, it seeks the largest simple path in the network of the solar system (the length of each path equals the number of its edges). To help him, you need to write a program that calculates the length of the longest simple path in such a network.
- INPUT
- Your program will read from the standard input, in the first line, a positive integer K number indicating the number of planets in the solar system. We can assume that the planets are numbered sequentially from 0 to K-1.
- The following 2K lines will provide a description of the K roads of the planets (in the form of the parent tables for the corresponding binary trees).
- In particular, line 2i will be given a positive integer N(i) indicating the number of countries on planet i.
- On line 2i+1, N(i)-1 physical numbers corresponding to the parent table of the binary tree of planet i will be given.
- In particular, we will always have the country number 1 as the root, and there will be no record on the parents' list for it.
- If the L-th position, L = 1, ..., N(i) - 1, is given the value J, this indicates that country L+1 of planet i has as its father on its road network country J.
- If N(i) = 1, i.e. when planet i has only one country, the root of the tree, line 2i+1 shall have a value of 0.
- OUTPUT
- Your program should print a positive integer in the standard output (in the first line) that corresponds to the length of the longest path in the transport network of the input
- CONSTRAINTS
- 3 <= K <= 10^5
- 1 <= N(i) <= 10^5
- 3 <= sum of N of all planets <= 2.000.000
- TIME LIMIT: 1 sec.
- Test Cases :https://drive.google.com/open?id=1DR6dvl0la4TH9zHHTYfdTjkiu7WM6-XM
Advertisement
Add Comment
Please, Sign In to add comment