bzoj1589/Usaco2008 DecTrick or Treat on the Farm 采集糖果——拓扑排序
Posted Child-Single
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj1589/Usaco2008 DecTrick or Treat on the Farm 采集糖果——拓扑排序相关的知识,希望对你有一定的参考价值。
Description
Input
Output
Sample Input
1 //1有一条边指向1
3 //2有一条边指向3
2 //3有一条边指向2
3
INPUT DETAILS:
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
Sample Output
2
2
3
HINT
Cow 1: Start at 1, next is 1. Total stalls visited: 1. Cow 2: Start at 2, next is 3, next is 2. Total stalls visited: 2. Cow 3: Start at 3, next is 2, next is 3. Total stalls visited: 2. Cow 4: Start at 4, next is 3, next is 2, next is 3. Total stalls visited: 3.
一般的做法就是trajan环缩点,但考虑到每个点的后继只有一个,那么环中的点就不存在一条边指向环外的点(基环树)。
那么如果我们把边反过来,从环开始逆拓扑一下,每往上走一步就答案+1即可。
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 #define mem(a,p) memset(a,p,sizeof(a)) 6 const int N=1e5+10; 7 int n,dfn[N],first[N],sz,dd[N],a[N]; 8 int cc[N],ne[N],q[N],t=0,h=1,an[N]; 9 bool ok[N]; 10 int read(){ 11 int ans=0,f=1;char c=getchar(); 12 while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} 13 while(c>=‘0‘&&c<=‘9‘){ans=ans*10+c-48;c=getchar();} 14 return ans*f; 15 } 16 int main(){ 17 int n=read(),tot=0; 18 for(int i=1;i<=n;i++){ 19 a[i]=read();ne[i]=first[a[i]];first[a[i]]=i;cc[i]++; 20 } 21 for(int i=1;i<=n;i++){ 22 if(ok[i])continue; 23 ok[i]=1;dfn[i]=++tot;dd[tot]=i;int to=a[i]; 24 while(!ok[to]){ 25 dfn[to]=++tot;dd[tot]=to; 26 ok[to]=1;to=a[to]; 27 } 28 sz=tot-dfn[to]+1; 29 for(int i=tot;i>=dfn[to];i--){ 30 int li=dd[i];cc[li]=0;an[li]=sz; 31 for(int j=first[li];j;j=ne[j])if(dfn[j]<dfn[to])q[++t]=j,an[j]=sz+1; 32 } 33 while(h<=t){ 34 int x=q[h];h++;ok[x]=1; 35 for(int i=first[x];i;i=ne[i]){ 36 an[i]+=an[x]+1; 37 cc[i]--;if(!cc[i])q[++t]=i; 38 } 39 } 40 } 41 for(int i=1;i<=n;i++)printf("%d\n",an[i]); 42 return 0; 43 }
以上是关于bzoj1589/Usaco2008 DecTrick or Treat on the Farm 采集糖果——拓扑排序的主要内容,如果未能解决你的问题,请参考以下文章
bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
bzoj1589/Usaco2008 DecTrick or Treat on the Farm 采集糖果——拓扑排序
bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果tarjan+记忆化搜索