BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
Description
每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大,所以约翰要想尽法子让奶牛们得到快乐.他给每一个牛棚设置了一个“后继牛棚”.牛棚i的后继牛棚是Xi.他告诉奶牛们,她们到了一个牛棚之后,只要再往后继牛棚走去,就可以搜集到很多糖果.事实上这是一种有点欺骗意味的手段,来节约他的糖果. 第i只奶牛从牛棚i开始她的旅程.请你计算,每一只奶牛可以采集到多少糖果.
Input
第1行输入N,之后一行一个整数表示牛棚i的后继牛棚Xi,共N行.
Output
共N行,一行一个整数表示一只奶牛可以采集的糖果数量.
题解:
是一个基环树森林,可以用基环树的方法瞎搞。
也可以求强联通分量乱搞。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
],X[],P[];
int tot;
inline void add(int x,int y){
P[++tot]=y;X[tot]=H[x];H[x]=tot;
}
];
];
];
];
];
];
];
int cnt,tim;
int top;
void tarjan(int x){
int dfn=low[x]=++tim;
stk[top++]=x;instk[x]=;
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]=;
belong[k]=cnt;
hav[cnt]++;
}while(x!=k);
}
}
int ask(int x){
if(ans[x]) return ans[x];
ans[x]=hav[x];
)
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);
;i<=n;i++){
scanf("%d",&to[i]);
}
;i<=n;i++){
if(!low[i]) tarjan(i);
}
;i<=n;i++){
if(belong[i]!=belong[to[i]]) add(belong[i],belong[to[i]]);
}
;i<=n;i++){
printf("%d\n",ask(belong[i]));
}
;
}
BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果的更多相关文章
- bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】
对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可 #include<iostream> #include<cstdio> using namesp ...
- 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 4 ...
- 【BZOJ】1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
[算法]基环树DP [题意]给定若干有向基环树,每个点能走的最远路径长度. [题解] 参考:[BZOJ1589]Trick or Treat on the Farm 基环树裸DP by 空灰冰魂 考虑 ...
- BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 4 ...
- bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
http://www.lydsy.com/JudgeOnline/problem.php?id=1589 tarjan缩环后拓扑排序上DP #include<cstdio> #includ ...
- 【强连通分量缩点】【记忆化搜索】bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
缩成DAG f(i)表示以i为起点的最长路 #include<cstdio> #include<cstring> #include<algorithm> #incl ...
- [BZOJ1589] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)
传送门 先用tarjan缩点,再记忆话搜索一下 #include <stack> #include <cstdio> #include <cstring> #inc ...
- LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
今天我来给大家带来一片蒟蒻题解 ~~真香 LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上 ...
- 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
[洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
随机推荐
- 【ANT】构建文件build.xml
运行ant: ant 使用当前目录下的build.xml运行ant,指定缺省的target; ant –buildfile mybuild.xml 使用当前目录下的mybuild.xml运行ant,并 ...
- HTML5新特性之WebNotifications
Web Notifications是HTML5中一个令人欣喜的新特性,它支持开发者配置和显示桌面通知,为用户提供更好的体验,最令人称赞的是,即使用户忙于其他工作时也可以收到来自页面的消息通知,例如一个 ...
- 关于Tesseract3.01的使用方法
Tesseract就不多介绍勒,能找到的人都知道是干嘛的 下面记录一下C# vs2010下的使用方法(借鉴http://blog.csdn.net/bobo1013767522/article/det ...
- ajax-典型应用-验证用户名
用servlet实现后台:
- Anroid之Intent的使用
inten常见动作:MAIN_ACTION(主视图). VIEW_ACTION(查看). EDIT_ACTION(修改). PICK_ACTION(选择) .GET_CONTENT_ACTION(获取 ...
- rowid
rowid:select t.*,t.rowid from test t; -- AACeJKAAIAAAA4XAAA 注:有个很二需求,通过一个存储过程插入的数据必须是放在一块的,不能分散开存储,考 ...
- vim全局替换
:1,$ s/cmss_//g 1,$代表第一行到最后一行 s代表替换 cmss_为要替换的内容 替换为空格,所以直接// g代表全局替换
- 找回mysql数据库密码
前提条件:你需要有数据库服务器的权限 1:修改my.ini配置文件 Mysqld:其中的d代表什么? Deamon后台运行的服务程序,增加一行跳过权限验证 2:停止mysql服务运行 3:启动mysq ...
- OpenCV3读取、写入和保存图像
需要说明的是在OpenCV3中已经将imread()和imwrite()函数转移到imgcodecs模块中,因此读写图像时,需要包含imgcodecs.hpp头文件,但是highgui.hpp头文件中 ...
- Angularjs在线编辑器
1.TextAngular: https://github.com/fraywing/textAngular textAngular是一个强大的Text-Editor/Wysiwyg 编辑器,用于An ...