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;
}
}
}

System. The out. Println (maxCircle);
}

CodePudding user response:

refer to the second floor Ragin 'Cajun thought 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;
}
}
}

System. The out. Println (maxCircle);
}

Thank you bosses ~ ~ ~ ~ ~ ~ ~ ~ ~

CodePudding user response:

I=nums [I - 1); Oh why

CodePudding user response:

refer to the second floor Ragin 'Cajun thought 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;
}
}
}

System. The out. Println (maxCircle);
}

I=nums [I - 1); Why do you do that?

CodePudding user response:

reference 4 floor qq_36943438 response:
I=nums [I - 1); Why ah

Find the people worship the children ah,

This code can also streamline, map that doesn't work, the map. The values () into nums,

CodePudding user response:

refer to 6th floor Ragin 'Cajun thought response:
Quote: refer to 4th floor qq_36943438 response:

I=nums [I - 1); Why ah

Find the people worship the children ah,

This code can also streamline, map that doesn't work, the map. The values () into nums,

That is why I - 1 is not his right hand can worship

CodePudding user response:

refer to 7th floor qq_36943438 response:
that is why I - 1? Is not his right hand can worship?

Look at the pictures of the single arrow,
1 children worship 22, then you will go to subscript 21, if he wrote 1 children worship 22 and 20, it should be ok according to your ideas,

CodePudding user response:

reference Ragin 'Cajun thought back on the eighth floor:
Quote: refer to 7th floor qq_36943438 response:

That is why I - 1 is not his right hand can worship?

Look at the pictures of the single arrow,
1 children worship 22, then you will go to subscript 21, if he wrote 1 children worship 22 and 20 that should write, can according to your ideas

Got it, thank you ~ ~ ~ ~ ~ ~ ~

CodePudding user response:

#include
#include

Int n, * a, * flag, head, Max, count;

Void init ();
Void BFS (int temp);

Int main () {
int i;
The init ();
for(i=0; iFlag [I]=1;//tag

The head=I + 1;
Count=0;
BFS (a [I]);

Flag [I]=0;//cancel the tag
}
Printf (" % d ", Max);
return 0;
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull