题目描述

每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的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.

输入输出样例

输入样例#1:

4
1
3
2
3
输出样例#1:

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的更多相关文章

  1. [USACO08DEC]Trick or Treat on the Farm 记忆化搜索

    这一题非常水,因为每个点的下一个目的地是唯一的,可以考虑对每一个还为访问过的点dfs直接找出所有的环,同时更新每一个点能去的点的数量(即答案). 我们dfs时找到环上已经遍历过的一个点,用当前的dfn ...

  2. [USACO08DEC]Trick or Treat on the Farm (拓扑排序,DP)

    题目描述 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大,所以约翰要想尽法子让奶牛们得到快乐. ...

  3. LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    今天我来给大家带来一片蒟蒻题解 ~~真香 LGOJ P2921  [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上 ...

  4. 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    [洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  5. 「USACO08DEC」「LuoguP2921」在农场万圣节Trick or Treat on the Farm(tarjan

    题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定 ...

  6. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  7. 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  8. C++ 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题解

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 分析: 这棵树上有且仅有一个环 两种情况: 1.讨论一个点在环上,如果在则答案与它指向点相同, 2 ...

  9. 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 ...

随机推荐

  1. [Mysql] 一些记录

    1> 修改表的字段 alter table trade_market change reqype reqtype int(10) unsigned not null;alter table tr ...

  2. openstack Icehouse发布

    OpenStack 2014.1 (Icehouse) Release Notes General Upgrade Notes Windows packagers should use pbr 0.8 ...

  3. Nginx模块之————RTMP模块的FFmpeg的配置问题是FFmpeg的连续退出

    rtmp { server { listen ; application live { allow publish all; allow play all; live on; exec /root/b ...

  4. tomcat配置项目的图片路径不在项目下的处理

    <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWA ...

  5. HBase之show table

    import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import or ...

  6. MySQL 死锁问题分析

    转载: MySQL 死锁问题分析 线上某服务时不时报出如下异常(大约一天二十多次):"Deadlock found when trying to get lock;". Oh, M ...

  7. Easy UI

    首先去Easy UI官网下载离线包 导入要用的js模块 <!DOCTYPE html> <html> <head lang="en"> < ...

  8. Python--关于连接符+

    连接符 + 连接符 + 实则是创建了新的对象并占用新的内存(dict.set不能使用) String 由于Python必须为每一个使用连接符+的字符串分配新的内存,并产生新的字符串.下面两种方式会更有 ...

  9. redhat网络基础配置

    添加浮动IP: ifconfig eth0:1 192.168.1.106 IP配置文件: BOOTPROTO: 采用的启动协议,有三种选择: (1) none:不使用启动协议 (2) static: ...

  10. C++数组小知识

    数组大小 我们一般情况下可以使用sizeof(数组名)/sizeof(数组元素)求数组元素个数,但需要注意的是,当我们需要调用函数处理数组时,数组的长度要在调用函数之前获取,因为调用函数的时候,数组退 ...