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< ...
随机推荐
- C#算法基础之冒泡排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- win7下将主分区转换成逻辑分区
在了解怎么转换之前,先搞清楚主分区,扩展分区,逻辑分区的基本概念. 主分区,也称为主磁盘分区,和扩展分区.逻辑分区一样,是一种分区类型.主分区中不能再划分其他类型的分区,因此每个主分区都相当于一个逻辑 ...
- [转]WCF 4 安全性和 WIF 简介
转自:http://www.cnblogs.com/WizardWu/archive/2010/10/04/1841793.html 本帖简介 .NET 新一代的 Windows Identity ...
- ios 解析json,xml
一.发送用户名和密码给服务器(走HTTP协议) // 创建一个URL : 请求路径 NSString *urlStr = [NSString stringWithFormat:@"ht ...
- (转)java:快速文件分割及合并
文件分割与合并是一个常见需求,比如:上传大文件时,可以先分割成小块,传到服务器后,再进行合并.很多高大上的分布式文件系统(比如:google的GFS.taobao的TFS)里,也是按block为单位, ...
- String的几种初始化方法的区别
参考了: java中String的两种初始化方法 String a; String aa = ""; String aaa = "123"; String ...
- Java编程思想之字符串
来自:Java编程思想(第四版) 第十三章 字符串 字符串操作是计算机程序中最常见的行为. String对象是不可变的.查看JDK文档你就会发现,String类中每一个看起来会修改String ...
- zabbix3.0.3 设置邮件报警
在zabbix3.0.3 设置报警这里卡了两天.终于解决了,这里我使用的mailx来作为发送邮件的客户端 1.设置mailx发信账号 yum -y install mailx ln -s /bin/m ...
- PHP取二进制文件头快速判断文件类型
<?php /*文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.d ...
- C# winfrom中的布局 控件Anchor和Dock的区别
c#中的布局问题 http://hi.baidu.com/whzpower/item/57e3179cca21e1cab725317a