【BZOJ1589】【USACO 2008 Dec Gold】 1.Trick or Treat on the Farm 基环树裸DP、
2015-01-02 23:01
567 查看
没测样例一遍A这真是……
题意:每个点都有且仅有一个出边(可以出现自环),然后这样一个点出发就会走过且一定走过某些点。
问每个点出发都会走过几个点。
首先这是基环树无疑。
然后就是裸DP了。
这个的关键就是找环,仅此。
题意:每个点都有且仅有一个出边(可以出现自环),然后这样一个点出发就会走过且一定走过某些点。
问每个点出发都会走过几个点。
首先这是基环树无疑。
然后就是裸DP了。
这个的关键就是找环,仅此。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 101000 using namespace std; int next ,f ; int stk ,top,n; int vis ; int main() { register int i,j,k; scanf("%d",&n); for(i=1;i<=n;i++)scanf("%d",&next[i]); 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] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)
- bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点+记忆化搜索)
- bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】
- BZOJ 1589: [Usaco2008 Dec]Trick 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 采集糖果
- 【BZOJ1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【SCC】【基环外向树】【DP】【记忆化搜索】
- 【bzoj1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- BZOJ 1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 tarjan+拓扑
- bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- [Usaco2008 Dec][BZOJ1589] Trick or Treat on the Farm 采集糖果
- 【强连通分量缩点】【记忆化搜索】bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- bzoj[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 tarjan
- 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- [USACO08DEC]Trick or Treat on the Farm
- 【bzoj1589/Usaco2008 Dec】Trick or Treat on the Farm 采集糖果——拓扑排序