[USACO08DEC] Trick or Treat on the Farm
题目描述
每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果.
农场不大,所以约翰要想尽法子让奶牛们得到快乐.他给每一个牛棚设置了一个“后继牛 棚”.牛棚i的后继牛棚是next_i 他告诉奶牛们,她们到了一个牛棚之后,只要再往后继牛棚走去, 就可以搜集到很多糖果.事实上这是一种有点欺骗意味的手段,来节约他的糖果.
第i只奶牛从牛棚i开始她的旅程.请你计算,每一只奶牛可以采集到多少糖果.
输入输出格式
输入格式:
Line 1: A single integer: N
- Lines 2..N+1: Line i+1 contains a single integer: next_i
输出格式:
- Lines 1..N: Line i contains a single integer that is the total number of unique stalls visited by cow i before she returns to a stall she has previously visited.
输入输出样例
4
1
3
2
3
1
2
2
3
说明
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
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.
- 本题n<=100000,首先考虑记忆化搜索,因为纯搜索在某些极端情况一定会被卡(n^2)。
- 但是记忆化搜索怎么实现呢?
- 通过观察样例发现,本题存在环。
- tarjan缩点+记忆化搜索。(大神说不用tarjan)。
- 所有环上的点的答案即为该环的长度,其余的点呢?因为其他的点都是指向某个环的,所以记忆化搜索即可(dp)。
- 期望得分100分。
#include <cstdio>
#include <algorithm>
#include <iostream>
#define time dscada
using namespace std; int n,l,cnt,top,next[],summ[],pre[],time,last[],other[],f[],dfn[],low[],stack[],huan[];
bool cir[],vis[]; void add(int u,int v) {
pre[++l]=last[u];
last[u]=l;
other[l]=v;
} void tarjan(int x) {
dfn[x]=low[x]=++time;
stack[++top]=x;
vis[x]=;
for (int p=last[x]; p; p=pre[p]) {
int q=other[p];
if (!dfn[q]) {
tarjan(q);
low[x]=min(low[x],low[q]);
} else if (vis[q]) low[x]=min(low[x],dfn[q]);
}
if (dfn[x]==low[x]) {
int sum=;
cnt++;
int now=stack[top--];
cir[now]=;
vis[now]=;
huan[now]=cnt;
sum++;
while (now!=x) {
now=stack[top--];
cir[now]=;
vis[now]=;
huan[now]=cnt;
sum++;
}
summ[cnt]=sum;
}
} int dfs(int x) {
if (f[x]) return f[x];
if (next[x]==x) {
f[x]=;
return f[x];
}
if (cir[x]) {
f[x]=summ[huan[x]];
return f[x];
}
if (cir[next[x]]) {
f[x]=summ[huan[next[x]]]+;
return f[x];
}
f[x]=dfs(next[x])+;
return f[x];
} int main() {
scanf("%d",&n);
for (int i=; i<=n; i++) {
int x;
scanf("%d",&x);
next[i]=x;
if (x!=i) add(i,x);//其实这里没有必要建边,用next数组即可。
}
for (int i=; i<=n; i++)
if (!dfn[i]) tarjan(i);//tarjan缩点
for (int i=; i<=n; i++) if (summ[huan[i]]==) cir[i]=;
//for (int i=1; i<=n; i++) printf("%d\n",cir[i]);
for (int i=; i<=n; i++) printf("%d\n",dfs(i));//记忆化搜索
return ;
}
[USACO08DEC] Trick or Treat on the Farm的更多相关文章
- [USACO08DEC]Trick or Treat on the Farm 记忆化搜索
这一题非常水,因为每个点的下一个目的地是唯一的,可以考虑对每一个还为访问过的点dfs直接找出所有的环,同时更新每一个点能去的点的数量(即答案). 我们dfs时找到环上已经遍历过的一个点,用当前的dfn ...
- [USACO08DEC]Trick or Treat on the Farm (拓扑排序,DP)
题目描述 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大,所以约翰要想尽法子让奶牛们得到快乐. ...
- 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< ...
- 「USACO08DEC」「LuoguP2921」在农场万圣节Trick or Treat on the Farm(tarjan
题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定 ...
- P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- C++ 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题解
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 分析: 这棵树上有且仅有一个环 两种情况: 1.讨论一个点在环上,如果在则答案与它指向点相同, 2 ...
- 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 ...
随机推荐
- [bzoj1452][JSOI2009]Count(树状数组)
1452: [JSOI2009]Count Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2057 Solved: 1216[Submit][Stat ...
- 如何将php的错误输出到nginx的error_log中去
参考文档:http://www.cnblogs.com/glory-jzx/p/3966082.html 通过FastCGI运行的PHP,在用户访问时出现错误,会首先写入到PHP的errorlog中如 ...
- vs2010 vc++ 统一修改所有工程的目录配置
vs2005和vs2008中都是通过 工具-选项-项目和解决方案-VC++目录,设置 头文件include .库文件lib.可执行文件dll的路径,以便在引用dll动态链接库文件时,可以查找到该文件的 ...
- python成长之路【第五篇】:python字符编码
在2.7环境中我们要写上这一行#-*- coding:utf-8 -*- 为什么我们要加这一行呢?这一样的意思是置顶编码类型为utf-8编码! 首先在看这个问题之前,咱们是否曾想过一个问题? 为什么我 ...
- Deep Learning 3_深度学习UFLDL教程:预处理之主成分分析与白化_总结(斯坦福大学深度学习教程)
1PCA ①PCA的作用:一是降维:二是可用于数据可视化: 注意:降维的原因是因为原始数据太大,希望提高训练速度但又不希望产生很大的误差. ② PCA的使用场合:一是希望提高训练速度:二是内存太小:三 ...
- Windows下配置OpenGL环境
这里编译工具为VS2012. 首先OpenGL的官网如下链接(英文) http://www.opengl.org http://www.opengl.org/resources/libraries/g ...
- "struct"类型重定义解决办法
#ifndef 在头文件中的作用 在一个大的软件工程里面,可能会有多个文件同时包含一个头文件,当这些文件编译链接成一个可执行文件时,就会出现大量 “重定义”的错误. 在头文件中使用#ifndef #d ...
- hdu 5877/ 2016 ACM/ICPC Dalian Online 1010 Weak Pair
题目链接 分析:树上的节点祖先与儿子的关系,一般就会想到dfs序.正解就是对树先进行dfs序排列,再将问题转化到树状数组统计个数.应该把节点按照权值从大到小排序,这样对于,就是从小到大的顺序.这样更新 ...
- NetworkComms V3 之同时监听多端口
NetworkComms网络通信框架序言 NetworkComms通信框架,是一款来自英国的c#语言编写的通信框架,历时6年研发,成熟稳定,性能可靠. 框架支持同时监听服务器上的多个端口,写法如下 ...
- Css中常用中文字体的Unicode编码对照
在网页制作中,最常用的恐怕是字体属性了,在调整页面兼容的时候,也常常发现字体名称的原因导致不兼容或乱码,下面给出几种常用字体的ucicode编码对照,方便使用. 宋体 SimSun \5B8B\4F5 ...