Home > Back-end > The ninth blue cup javaC group 9 questions - the children worship circle, pursuing big brand me,
The ninth blue cup javaC group 9 questions - the children worship circle, pursuing big brand me,
Time:10-06
Topic: children worship circle
N of children in the class, everyone has their favorite a friend (can be), In a game, kids need to sit a circle, Each kid has their own favorite kid in his right hand, How many people to meet the conditions of the largest circle?
Children Numbers for 1, 2, 3,... N Enter the first line, an integer N (3 & lt; N<100000) The next line of N integers, separated by Spaces,
Request output an integer, says the biggest circle the number of people meet the conditions,
For example: Input: 9 4, 3, 4, 8 2 5 3 6 9
The program should output: 4
Explanation: As shown in figure (p1) shown in PNG, worship relationship with arrows indicate, red is not in circle, Obviously, the largest circle are [2, 4, 5] circle
For example: Input: 30 22 to 28 16 June 27 21 30 1 and 10 September 14 26 April 12 24 November 7 8 2 5 3 25 23 17 18 19 20 13 15
The program should output: 16
CodePudding user response:
The appended drawings
CodePudding user response:
Public void the run () { Int [] nums={22 to 28, 16, 6, 27, 21, 30, 1, 29, 10, 9, 14, 24, 11, 7, 2, 8, 5, 26, 4, 12, 3, 25, 18, 20, 19, 23, 17, 13, 15};
Map The map=new HashMap<> (a); For (int I=0; iThe map. The put (I, nums [I]); }
The Integer maxCircle=0; For (Integer v: map values ()) { List Group=Lists. NewArrayList (); Int nodeCount=0; Int I=v;
while (! Group. The contains (I)) { Group. The add (I); I=nums [I - 1); NodeCount + +; If (nodeCount & gt; MaxCircle) { MaxCircle=nodeCount; } } }