P2661 Information Transfer
-
- Topic provider The user does not exist
- Tag graph on linear structure Noip raising group
- Difficulty popularization/improvement-
Submit a discussion of the problem record
Recent discussions
- On their own computer with official data evaluation full ...
- P Party last point timeout yyyyyyy ...
- Pascal Party, can only take 70, the last three ...
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 and Output Sample input example # #:
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.
Analysis: Last year to participate in Noip when nothing, this problem think of a very strange method, and then zero, in fact, this problem test instructions very clear, is to let us find a minimum ring length, how to ask? It is also very simple, first of all the points in the degree of 0 is deleted, and then the point points to the degree of 1, if the degree of 0, also deleted, so that only the useful points, then from any point, with a vis array record is accessed, access to a new node on the accumulation counter, And then it's done. BFS and DFS are all available.
#include <cstdio>#include<cstring>#include<queue>#include<iostream>#include<algorithm>using namespacestd;intto[200010],vis[200010], rubian[200010];intN,ans;queue<int>Q;intMain () {memset (To,0,sizeof(to)); memset (Vis,0,sizeof(VIS)); memset (Rubian,0,sizeof(Rubian)); scanf ("%d", &N); Ans=N; for(inti =1; I <= N; i++) {scanf ("%d", &To[i]); ++Rubian[to[i]]; } for(inti =1; I <= N; i++) if(Rubian[i] = =0) {Q.push (i); Vis[i]=1; } while(!Q.empty ()) { intU =Q.front (); Q.pop (); --Rubian[to[u]]; if(Rubian[to[u]] = =0) {Vis[to[u]]=1; Q.push (To[u]); } } inttemp,j; for(inti =1; I <= N; i++) { if(Vis[i] = =0&& Rubian[i]! =0) {Vis[i]=1; Temp=1; J=To[i]; while(!Vis[j]) {Vis[j]=1; J=To[j]; Temp++; } if(Temp <=ans) ans=temp; }} printf ("%d\n", ans); return 0;}
noip2014 Information Transfer