Title Description
Every Halloween, Wisconsin cows dress up and walk around the farm's N-barn to collect sweets. Each of them goes to a barn that has never passed and collects 1 candies from the shed.
The farm is not big, so John will try his best to make the cows happy. He set up a "successor barn" to every barn. The successor of the barn I was next_i he told the cows that after they had reached a barn, they had to go to the next barn, You can collect a lot of sweets. In fact, it is a kind of deceptive means to save his candy.
The first cow starts her journey from the barn I. Please calculate how many candies each cow can collect.
Input/output format
Input format:
Output format:
- Lines 1..n:line I contains a single integer that's the total number of unique stalls visited by cow I before she returns To a stall she had previously visited.
Input and Output Sample input example # #:
Sample # # of output:
Description
Four stalls.
Stall 1 directs the cow back to Stall 1.
Stall 2 directs the cow to Stall 3
Stall 3 directs the cow to Stall 2
- Stall 4 directs the cow to Stall 3
Cow 1:start at 1, next is 1. Total Stalls visited:1.
Cow 2:start at 2, Next was 3, next is 2. Total stalls Visited:2 Cow 3:start at 3, Next was 2, next is 3. Total stalls visited:2 Cow 4:start at 4, Next was 3, Next was 2, next is 3. Total Stalls visited:3.
- The subject n<=100000, first consider the memory search, because the pure search in certain extreme situations will certainly be stuck (n^2).
- But how does the memory search come true?
- By observing the sample, we find that there is a ring.
- Tarjan indent + memory search. (the Great God said no Tarjan).
- The answer to all the points on the ring is the length of the ring, and the rest of the points? Because the other points are pointing to a ring, the memory search can be (DP).
- Expect to score 100 points.
1#include <cstdio>2#include <algorithm>3#include <iostream>4 #defineTime Dscada5 using namespacestd;6 7 intn,l,cnt,top,next[100050],summ[100050],pre[100050],time,last[100050],other[100050],f[100050],dfn[100050],low[100050],stack[100050],huan[100050];8 BOOLcir[100050],vis[100050];9 Ten voidAddintUintv) { Onepre[++l]=Last[u]; Alast[u]=l; -other[l]=v; - } the - voidTarjan (intx) { -dfn[x]=low[x]=++Time ; -stack[++top]=x; +vis[x]=1; - for(intP=LAST[X]; P p=Pre[p]) { + intq=Other[p]; A if(!Dfn[q]) { at Tarjan (q); -low[x]=min (low[x],low[q]); -}Else if(Vis[q]) low[x]=min (low[x],dfn[q]); - } - if(dfn[x]==Low[x]) { - intsum=0; incnt++; - intnow=stack[top--]; tocir[now]=1; +vis[now]=0; -huan[now]=CNT; thesum++; * while(now!=x) { $now=stack[top--];Panax Notoginsengcir[now]=1; -vis[now]=0; thehuan[now]=CNT; +sum++; A } thesumm[cnt]=sum; + } - } $ $ intDfsintx) { - if(F[x])returnF[x]; - if(next[x]==x) { thef[x]=1; - returnF[x];Wuyi } the if(Cir[x]) { -f[x]=Summ[huan[x]]; Wu returnF[x]; - } About if(Cir[next[x]]) { $f[x]=summ[huan[next[x]]]+1; - returnF[x]; - } -F[x]=dfs (Next[x]) +1; A returnF[x]; + } the - intMain () { $scanf"%d",&n); the for(intI=1; i<=n; i++) { the intx; thescanf"%d",&x); thenext[i]=x; - if(x!=i) Add (i,x);//There is no need to build edges here, use the next array. in } the for(intI=1; i<=n; i++) the if(!Dfn[i]) Tarjan (i);//tarjan Reduction Point About for(intI=1; i<=n; i++)if(summ[huan[i]]==1) cir[i]=0; the //for (int i=1; i<=n; i++) printf ("%d\n", Cir[i]); the for(intI=1; i<=n; i++) printf ("%d\n", DFS (i));//Memory Search the return 0; +}
[Usaco08dec] Trick or Treat on the Farm