Information Transfer Topic 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.
The 2nd line contains n spaces separated by a positive integer t1,t2,......, tn where the i-integer Ti shows the students with number I the information passing object is a classmate numbered Ti, 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 and Output Sample input example # #:
5 2 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.
My puzzle.
Obviously, this problem can be converted to the question of finding the smallest ring in graph theory.
(originally also found a picture of the graph of software, and later found that the function of ppt is strong enough = =)
Each classmate as a node, I and TI are connected to the edge.
Last year's brush to this problem has always felt that the characteristics of the ring is not obvious, this year to figure theory to understand a bit more deeply, it is better ...
There are many ways to find a ring, and here are two kinds of words.
Method One: Topological ordering
When the topology is sorted, the points in the 0 are constantly deleted, but the points in the ring are never 0, so the ring is preserved.
Search for the smallest length in the remaining rings with this property.
Method Two: Finding strong connected components
Because each classmate has only one fixed object, so each node has a degree of 1. The strong connected component is equal to the seeking loop at this time.
Silly x Error: This picture may not be connected! Like what:
So need to cycle tarjan...o (╯-╰) o
My Code
Method One:
1#include <iostream>2#include <cstdio>3#include <queue>4#include <algorithm>5 using namespacestd;6 intN;7 intans=10000000;8 structnode9 {Ten intNext; One intCNT; A BOOLBJ; -Node () {next=0; cnt=0; bj=0;} -}a[200001]; theQueue <node>que; - intMain () - { -Cin>>N; + for(intI=1; i<=n;i++) - { +Cin>>A[i].next; Aa[a[i].next].cnt++;//Record Entry degree at } - //topology - for(intI=1; i<=n;i++) - { - if(a[i].cnt = =0) - { in Que.push (A[i]); -a[i].bj=1; to } + } - while(!que.empty ()) the { *Node i=Que.front (); $ Que.pop ();Panax Notoginseng if(--a[i.next].cnt==0) - { the Que.push (A[i.next]); +a[i.next].bj=1; A } the } + - for(intI=1; i<=n;i++) $ { $ if(a[i].bj==0) - { - intJ=i,count=0; the Do - {Wuyicount++; thea[j].bj=1; -j=A[j].next; Wu} while(j!=i); -ans=min (ans,count); About } $ } -cout<<ans; - return 0; -}
Click to send dragon Slayer Blades
Method Two:
1 /*2 */3#include <iostream>4#include <cstdio>5#include <cstring>6#include <vector>7 using namespacestd;8 9 intN,scc=0, idx=0, s[200700],low[200700],dfn[200700],top=0; Ten BOOLin_stack[200700]; One intans=10000000; Avector<int> edge[200700]; - intlength[200700]; - the voidTarjan (intu) { -Dfn[u] = low[u] = ++idx;//Init -s[top++] = u;//Press Stack -In_stack[u] =true; + for(inti =0; I < edge[u].size (); i++) { - intv =Edge[u][i]; + if(!dfn[v]) {//has not been visited ATarjan (v);//Continue atLow[u] =min (Low[u], low[v]); - } - Else if(In_stack[v]) {//if it's still inside the stack -Low[u] =min (Low[u], dfn[v]); - } - } in intSize=0; - if(Dfn[u] = = Low[u]) {//if u is the root of the strong Unicom to Do { +size++; -In_stack[s[--top]] =false;//Back Stack the} while(S[top]! = u);//walk through a ring back * if(Size! =1) $ans=min (ans,size);Panax Notoginseng } - } the intMain () { +scanf"%d",&n); A for(intI=1; i<=n;i++){ the intv; +scanf"%d",&v); - Edge[i].push_back (v); $ } $ for(intI=1; i<=n;i++) - Tarjan (i); -printf"%d", ans); the return 0; -}
Click to send dragon Slayer Blades
"Topological sort/strong Unicom component" "NOIP201402" information passing