Topic links: https://www.luogu.org/problem/P2661
Meaning of the questions: Everyone has a passing objects, at the beginning of each people only know their birthday, every one will know his own birthday told to pass an object, when his birthday hear from others at the end of the game, to inquire you can at least play a few rounds the game.
Analysis: is to find the smallest ring, dfs do, two arrays vis mark (code to a record as used), used an array of belonging vis conventional array, labeling this point has been visited, do not go, vis array is to find the smallest ring, arranged in no time lookup. Specific look at the code
#include <bits / STDC ++ H.> the using namespace STD; typedef Long Long LL; const int MAXN = 2E5 + . 7 ; const int N = MAXN * 20 is ; const LL INF = 0x3f3f3f3f ; #define MEM0 (A) Memset (A, 0 , the sizeof (a)) int D [MAXN]; int ANS = INF; int STEP [MAXN]; // for recording the number of sides passes through a first point in a loop int VIS [MAXN], used [MAXN]; // VIS recording array has passed this point lookup (used to determine whether the ring), used to mark the point used array has gone through once, do not go any farther void DFS ( intNd, int NUM) { // Nd, NUM respectively to the current node and the number of steps walked IF (Used [Nd]) return ; IF (VIS [Nd]) { ANS = min (ANS, num- STEP [Nd]); } the else { VIS [Nd] = . 1 ; STEP [Nd] = NUM; DFS (D [Nd], NUM + . 1 ); Used [Nd] = . 1 ; } } int main () { int n- ; Scanf ( " % D " , & n-); for (int i=1;i<=n;i++){ scanf("%d",&d[i]); } for(int i=1;i<=n;i++){ dfs(i,0); } cout<<ans<<endl; return 0; }