Description
Every Halloween, cows in Wisconsin have to dress up and wander out in the farm's n (1 ≤ n ≤ 100000) sheds to collect candy. every time they walk to an unapproved cowshed, they will collect a candy in the shed. john tried his best to make the cows happy. he set up a "next-generation cowshed" for every cowshed ". the successor of Cowshed I is Xi. he told the cows that after they arrived at a cowshed, they could collect a lot of sweets as long as they followed the cowshed. in fact, this is a bit of a deception to save his candy. the I-th cow started her journey from the cowshed. calculate how much candy each cow can collect. input
Input N in the second row, and an integer in the second row indicates the successor bucket XI of the bucket I, n rows in total. Output
There are n rows in total. An integer in one row indicates the number of Sweets that a cow can collect.
Question:
It is a base ring tree forest. You can use the base ring tree method.
You can also make sure that China Unicom's components are messy.
Code:
#include<cstdio>#include<cstring>#include<algorithm>//by zrt//problem:using namespace std;int H[100005],X[100005],P[100005];int tot;inline void add(int x,int y){ P[++tot]=y;X[tot]=H[x];H[x]=tot;}long long ans[100005];int low[100005];int stk[100005];bool instk[100005];int to[100005];int belong[100005];int hav[100005];int cnt,tim;int top;void tarjan(int x){ int dfn=low[x]=++tim; stk[top++]=x;instk[x]=1; if(!low[to[x]]){ tarjan(to[x]); low[x]=min(low[x],low[to[x]]); }else if(instk[to[x]]) low[x]=min(low[x],low[to[x]]); if(dfn==low[x]){ int k; cnt++; do{ k=stk[--top]; instk[k]=0; belong[k]=cnt; hav[cnt]++; }while(x!=k); }}int ask(int x){ if(ans[x]) return ans[x]; ans[x]=hav[x]; if(hav[x]==1) ans[x]+=ask(P[H[x]]); return ans[x];}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&to[i]); } for(int i=1;i<=n;i++){ if(!low[i]) tarjan(i); } for(int i=1;i<=n;i++){ if(belong[i]!=belong[to[i]]) add(belong[i],belong[to[i]]); } for(int i=1;i<=n;i++){ printf("%d\n",ask(belong[i])); } return 0;}
Bzoj 1589: [usaco DEC] trick or treat on the farm