No test sample again A This is really ...
Test instructions: Each point has only one out edge (which can appear as a self-loop), and then a point starts over and must pass certain points.
Ask each point to go through a few points.
First of all this is the base ring tree undoubtedly.
And then there's the bare DP.
The key to this is to find the ring, only this.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 101000using namespace Std;int next[n],f[n];int stk[n],top,n;int vis[n];int Main () {register int i,j,k;scanf ("%d", &n ), for (i=1;i<=n;i++) scanf ("%d", &next[i]), and for (i=1;i<=n;i++) if (!vis[i]) {top=0;for (j=i;! Vis[j];j=next[j]) {stk[++top]=j;vis[j]=i;} if (vis[j]==i) {int tt=top;while (stk[tt--]!=j); tt=top-tt;do{f[stk[top--]]=tt;} while (STK[TOP+1]!=J);} K=0;while (top) f[stk[top--]]=f[j]+ (++k);} for (i=1;i<=n;i++) printf ("%d\n", F[i]); return 0;}
"BZOJ1589" "Usaco Dec Gold" 1.Trick or Treat on the Farm base ring tree bare DP,