Title Description
There are n classmates (numbered 1 to N) playing a message-passing game. In the game each person has a fixed message to pass the object, wherein, number I of the classmate's information Transfer object is numbered TI classmate.
At the beginning of the game, everyone only knew their birthdays. In each round, everyone will tell their own information about their current birthday message (note: Someone may get information from several people, but each person will only tell one person, that is, their message). The game is over when someone learns their birthday from someone else's mouth. How many rounds can the game take?
Input/output format
Input Format:
Enter a total of 2 rows.
Line 1th contains 1 positive integers n for n people.
Line 2nd contains n spaces separated by a positive integer t1,t2,......, tn where the i integer TI shows number i
The students of the information Transfer object is numbered Ti classmate, Ti≤n and Ti≠i
Data guarantee the game will end.
output Format:
Output a total of 1 lines, containing 1 integers, which indicates how many rounds the game can carry out altogether.
Input/Output sample
Input Sample # #:
52 4 2) 3 1
Sample # # of output:
3
Description
Example 1 explanation
The process of the game. After the 3rd round of games, player number 4th will hear that player number 2nd tells him
Birthday, so the answer is 3. Of course, after the 3rd round of games, player number 2nd and player 3rd will be able to get a message from themselves
Sources know that their birthdays are also eligible for the end of the game.
For 30% of data, n≤200;
For 60% of data, n≤2500;
For 100% of data, n≤200000.
Run one side Tarjan, and find the minimum (but not equal to 1) point of the indentation, which contains the number of points is the answer.
Because each point out of a maximum of 1, so do not need to use a complex way to save edges, only the number of groups to record the whereabouts of each point on the line.
1 /**/2#include <iostream>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <algorithm>7 using namespacestd;8 Const intmxn=230000;9 intV[MXN];Ten intDtime=0; One BOOLINST[MXN]; A intSt[mxn],top; - intLOW[MXN],DFN[MXN]; - // the intBelone[mxn],cnt=0; - intDG[MXN]; - // - intN; + voidTarjan (intu) { -low[u]=dfn[u]=++Dtime; +st[++top]=u; Ainst[u]=1; at // - if(!Dfn[v[u]]) { - Tarjan (V[u]); -low[u]=min (Low[u],low[v[u]]); - } - Else if(Inst[v[u]]) inlow[u]=min (Low[u],dfn[v[u]]); - // to if(low[u]==Dfn[u]) { +++CNT; - intW; the Do{ *w=st[top--]; $dg[cnt]++;Panax Notoginsenginst[w]=0; -} while(w!=u); the } + return; A } the intMain () { +scanf"%d",&n); - inti,j; $ for(i=1; i<=n;i++){ $scanf"%d",&v[i]); - } - for(i=1; i<=n;i++){ the if(!Dfn[i]) Tarjan (i); - }Wuyi intans=5000000; the for(i=1; i<=cnt;i++) {if(Dg[i]<ans && dg[i]!=1) ans=dg[i];} -printf"%d\n", ans); Wu return 0; -}
NOIP2015 Improve group T2 Rokua P2661 Information transfer