【BZOJ1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【SCC】【基环外向树】【DP】【记忆化搜索】
来源:互联网 发布:易语言csol游戏源码 编辑:程序博客网 时间:2024/06/06 14:02
http://www.lydsy.com/JudgeOnline/problem.php?id=1589
首先这是一个基环外向树,先tarjan缩点变成DAG,然后跑dp就行了。
记忆化搜索写成了
if(dp[x]) return x;
一定是没睡好...
/* Footprints In The Blood Soaked Snow */#include <cstdio>#include <algorithm>using namespace std;const int maxn = 100005;int n, dp[maxn], size[maxn], belong[maxn], next[maxn], to[maxn], dfn[maxn], low[maxn], clo, tot;bool ins[maxn];inline int iread() {int f = 1, x = 0; char ch = getchar();for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';return f * x;}int sta[maxn], top;inline void tarjan(int x) {dfn[x] = low[x] = ++clo;ins[sta[++top] = x] = 1;if(!dfn[next[x]]) tarjan(next[x]), low[x] = min(low[x], low[next[x]]);else if(ins[next[x]]) low[x] = min(low[x], dfn[next[x]]);if(low[x] == dfn[x]) {tot++;while(1) {int u = sta[top--];belong[u] = tot;size[tot]++;ins[u] = 0;if(u == x) break;}}}inline int dfs(int x) {if(dp[x]) return dp[x];dp[x] = size[x];if(to[x]) dp[x] += dfs(to[x]);return dp[x];}int main() {n = iread();for(int i = 1; i <= n; i++) next[i] = iread();for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i);for(int i = 1; i <= n; i++) if(belong[i] != belong[next[i]]) to[belong[i]] = belong[next[i]];for(int i = 1; i <= n; i++) printf("%d\n", dfs(belong[i]));return 0;}
0 0
- 【BZOJ1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【SCC】【基环外向树】【DP】【记忆化搜索】
- bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点+记忆化搜索)
- BZOJ 1589: [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 采集糖果
- [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
- bzoj[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 tarjan
- BZOJ 1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 tarjan+拓扑
- 【BZOJ1589】【USACO 2008 Dec Gold】 1.Trick or Treat on the Farm 基环树裸DP、
- 洛谷P2921/BZOJ1589[USACO08DEC]在农场万圣节Trick or Treat on the Farm
- 洛谷P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
- 洛谷 2921 记忆化搜索 tarjan 基环外向树
- 1621: [Usaco2008 Open]Roads Around The Farm分岔路口(记忆化搜索)
- Tyvj 题目1088 treat(DP+记忆化搜索)
- 3592 - Trick or Treat!
- POJ3873 Trick or Treat
- Trick or Treat
- PKU Trick or Treat
- python 类中的私有变量
- LINQ(LINQ to Entities)
- easyui中的datagrid组件前台分页刷新但是数据不刷新
- 一个用来获取硬盘信息工具的网站
- VC自动化向导的bug2(vs2005使用Labview控件类时的错误)
- 【BZOJ1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【SCC】【基环外向树】【DP】【记忆化搜索】
- 数据库密码过期 怎么修改
- 数据结构课设 符号配对
- 在eclipse中设计BPMN 2.0工作流定义的根本步骤
- D13
- excelhelp类
- Oracle密码过期 怎么修改
- Java ClassLoader 原理详细分析
- 学习JAVA之路(五、if、switch训练篇)