题目描述

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

    svn server: 1.c:\Program Files\TortoiseSVN\bin>svnserve -d -r C:\Jasper\Repositories2.change the ...

  2. TCP连接的状态与关闭方式及其对Server与Client的影响

    TCP连接的状态与关闭方式及其对Server与Client的影响 1. TCP连接的状态 首先介绍一下TCP连接建立与关闭过程中的状态.TCP连接过程是状态的转换,促使状态发生转换的因素包括用户调用. ...

  3. js正则表达式入门

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. urlencode 和 rawurlencode 的区别

    urlencode和rawurlencode的区别urlencode和rawurlencode的区别 $str1 = urlencode(':/?= &#'); $str2 = rawurle ...

  5. daterangepicker 双日历/格式化日期/日期限制minDate,maxDate

    var locale = { "format": 'YYYY/MM/DD', "separator": " - ", "apply ...

  6. 【xsy1019】小A的树论

    题意 三种操作: ①区间增值 ②某一个点换父亲 ③求子树和 \(1\leq n\leq 100000\) 分析 Splay维护dfn序. 小结 (1)使用Splay,通常要在Splay的两端各添加一个 ...

  7. org/objectweb/asm/Type异常解决办法

    关于java.lang.NoClassDefFoundError: org/objectweb/asm/Type 调试SPRING MVC(或者整合SSH)的时候遇到了org/objectweb/as ...

  8. AC自动机最好讲解

    http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf

  9. WCF初探-10:WCF客户端调用服务

    创建WCF 服务客户端应用程序需要执行下列步骤: 获取服务终结点的服务协定.绑定以及地址信息 使用该信息创建 WCF 客户端 调用操作 关闭该 WCF 客户端对象 WCF客户端调用服务存在以下特点: ...

  10. LeetCode 176 Second Highest Salary mysql,select 嵌套 难度:1

    https://leetcode.com/problems/second-highest-salary/ Write a SQL query to get the second highest sal ...