1589: [usaco DEC] trick or treat on the farm acquisition candy time limit: 5 sec memory limit: 64 MB
Submit: 400 solved: 220
[Submit] [Status] Description every Halloween, cows in Wisconsin will 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 1st rows enter N, And the next row has an integer representing the subsequent ox shed Xi, N rows in total. there are n rows in output, and an integer in one row indicates the number of Sweets that a cow can collect. sample input4 // there are four points
1 // 1. An edge points to 1.
3 // 2 There is an edge pointing to 3
2 // 3 has an edge pointing to 2
3
Input details:
Four stils.
* 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
Sample output1
2
2
3
Hint
Cow 1: Start at 1, next is 1. total stallvisited: 1. cow 2: start at 2, next is 3, next is 2. total stallvisited: 2. cow 3: start at 3, next is 2, next is 3. total stallvisited: 2. cow 4: Start at 4, next is 3, next is 2, next is 3. total stallvisited: 3.
Source
Gold
Question:
First, Tarjan, and then if the answer in a ring> 1 is definitely the size of the ring, otherwise it is the ans of 1 +.
Code:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 500+10014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26 int x=0,f=1;char ch=getchar();27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}29 return x*f;30 }31 int n,ti,tot,top,cnt;32 int go[100005],sta[100005],dfn[100005],low[100005];33 int head[100005],s[100005],scc[100005],ans[100005];34 struct data{int go,next;}e[100005];35 inline void insert(int x,int y)36 {37 e[++tot].go=y;e[tot].next=head[x];head[x]=tot;38 }39 inline void dfs(int x)40 {41 dfn[x]=low[x]=++ti;sta[++top]=x;42 if(!dfn[go[x]]){dfs(go[x]);low[x]=min(low[x],low[go[x]]);}43 else if(!scc[go[x]])low[x]=min(low[x],dfn[go[x]]);44 if(low[x]==dfn[x])45 {46 cnt++;int now=0;47 while(now!=x)48 {49 now=sta[top--];50 scc[now]=cnt;51 s[cnt]++;52 }53 }54 }55 inline int solve(int x)56 {57 if(ans[x])return ans[x];58 ans[x]=s[x];59 if(s[x]==1)ans[x]+=solve(e[head[x]].go);60 return ans[x];61 }62 int main()63 {64 freopen("input.txt","r",stdin);65 freopen("output.txt","w",stdout);66 n=read();67 for1(i,n)go[i]=read();68 for1(i,n)if(!dfn[i])dfs(i);69 for1(i,n)if(scc[i]!=scc[go[i]])insert(scc[i],scc[go[i]]);70 for1(i,n)printf("%d\n",solve(scc[i]));71 return 0;72 }
View code
Bzoj1589: [usaco2008 DEC] trick or treat on the farm