题意翻译

题目描述

每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节。

由于牛棚不太大,FJ通过指定奶牛必须遵循的穿越路线来确保奶牛的乐趣。为了实现这个让奶牛在牛棚里来回穿梭的方案,FJ在第i号隔间上张贴了一个“下一个隔间”Next_i(1<=Next_i<=N),告诉奶牛要去的下一个隔间;这样,为了收集它们的糖果,奶牛就会在牛棚里来回穿梭了。

FJ命令奶牛i应该从i号隔间开始收集糖果。如果一只奶牛回到某一个她已经去过的隔间,她就会停止收集糖果。

在被迫停止收集糖果之前,计算一下每头奶牛要前往的隔间数(包含起点)。

输入格式

第1行 整数n。

第2行到n+1行 每行包含一个整数 next_i 。

输出格式

n行,第i行包含一个整数,表示第i只奶牛要前往的隔间数。

样例解释

有4个隔间

隔间1要求牛到隔间1

隔间2要求牛到隔间3

隔间3要求牛到隔间2

隔间4要求牛到隔间3

牛1,从1号隔间出发,总共访问1个隔间;

牛2,从2号隔间出发,然后到三号隔间,然后到2号隔间,终止,总共访问2个隔间;

牛3,从3号隔间出发,然后到2号隔间,然后到3号隔间,终止,总共访问2个隔间;

牛4,从4号隔间出发,然后到3号隔间,然后到2号隔间,然后到3号隔间,终止,总共访问3个隔间。

翻译提供者:吃葡萄吐糖

题目描述

Every year in Wisconsin the cows celebrate the USA autumn holiday of Halloween by dressing up in costumes and collecting candy that Farmer John leaves in the N (1 <= N <= 100,000) stalls conveniently numbered 1..N.

Because the barn is not so large, FJ makes sure the cows extend their fun by specifying a traversal route the cows must follow. To implement this scheme for traveling back and forth through the barn, FJ has posted a 'next stall number' next_i (1 <= next_i <= N) on stall i that tells the cows which stall to visit next; the cows thus might travel the length of the barn many times in order to collect their candy.

FJ mandates that cow i should start collecting candy at stall i. A cow stops her candy collection if she arrives back at any stall she has already visited.

Calculate the number of unique stalls each cow visits before being forced to stop her candy collection.

POINTS: 100

输入格式

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


题解

这个题虽然是图的题目,但是它的图很特殊,每个节点的出度都是1,而且一定存在环。

直接暴力可以得40分。

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h> using namespace std; const int MAXN = 1e5 + ;
int n, next[MAXN], vis[MAXN], ans[MAXN], p; int main()
{
cin >> n;
for(int i = ; i <= n; i++)
{
cin >> next[i];
}
for(int i = ; i <= n; i++)
{
memset(vis, , sizeof(vis));
ans[i] = ;
p = i;
vis[p] = ;
while(vis[next[p]] == )
{
vis[next[p]] = ;
p = next[p];
ans[i]++;
}
}
for(int i = ; i <= n; i++)
{
cout << ans[i] << endl;
} return ;
}

剩下的问题就是如何解决TLE的问题。和01迷宫问题的思路类似,需要对图进行染色,找到环,同一个环上的各个节点的隔间数是相同的,把这个结果保存下来就可以减少后续的运算量了。下图是样例染色的结果。1号节点是个自环长度为1,2和3号节点是长度为2的环,所以这两个节点输出的隔间数都是环的长度2。4号节点经过1步走到2和3组成的环,所以它的隔间数是1+2=3。

染色的过程有两种可能:

1)染色的过程遇到了同种颜色的节点,例如下图中2-3-4-3。

2)染色的过程中遇到了不同颜色的节点,例如下图中的5-2和6-4。

为了方便计算环的长度和走过的隔间数,我们引入两个变量:cnt和dfn数组。cnt从0开始,用于计算此次染色所经过的隔间数,每走一次加1。dfn记录每个节点和此次染色第一个节点之间的距离。例如第一种染色情况,dfn(2)=0,dfn(3)=1,dfn(4)=2。当从4走到3时,我们立刻可以发现一个同色节点组成的环。环的长度是cnt-dfn(3)=2。而且我们可以记录下这个颜色路径进入环的第一个节点是3号节点。

对于第2种染色过程,又分为两种情况:

1)所遇到的不同颜色的节点在环上,如4号节点。这种情况隔间数等于cnt+环长。

2)所遇到的不同颜色的节点不在环上,如2号节点。这种情况隔间数等于cnt+环长+从所遇到的节点到第一个进入环的节点的距离。

而判断节点是否在环上,可以用dfn的大小进行比较,所有比第一个进入环的节点的dfn小的节点都在环外,反之在环上。

下面就是改进后的代码:

 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h> using namespace std; const int MAXN = 1e5 + ;
int n, next[MAXN], vis[MAXN], ans[MAXN], p;
int dfn[MAXN], cnt;
int looplen[MAXN]; // 环长
int stloop[MAXN]; // 路径中进入环的第一个节点的dfn值 int main()
{
cin >> n;
for(int i = ; i <= n; i++)
{
cin >> next[i];
} memset(vis, , sizeof(vis));
for(int i = ; i <= n; i++)
{
cnt = ;
p = i;
while(vis[p] == )
{
vis[p] = i;
dfn[p] = cnt;
p = next[p];
cnt++;
}
if(vis[p] == i)
{ // 走到了自己染色的环
ans[i] = cnt;
looplen[i] = cnt - dfn[p];
stloop[i] = dfn[p];
}
else
{ // 走到了其他点染色的路径
looplen[i] = looplen[vis[p]];
stloop[i] = cnt;
if(stloop[vis[p]] > dfn[p])
{ // 遇到的点不在环上
stloop[i] += stloop[vis[p]] - dfn[p];
}
ans[i] = stloop[i] + looplen[i];
}
}
for(int i = ; i <= n; i++)
{
cout << ans[i] << endl;
} return ;
}

洛谷 P2921 在农场万圣节Trick or Treat on the Farm题解的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. [USACO08DEC]在农场万圣节Trick or Treat on the Farm【Tarja缩点+dfs】

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

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

    题目大意:给你一张有向图,每个点最多一条出边,问从每个开始,走多少步会到一个已经过的点 题解:$tarjan$缩点,然后建反图$DP$ 卡点:无 C++ Code: #include <cstd ...

随机推荐

  1. The last packet successfully received from the server was 1,480 milliseconds ago.

    场景:一个上传接口,需要上传几十M的文件,文件中包含10几W的数据,然后对10+W的数据进行同步批量插入,每次批量插入1W.最后返回结果. 项目上线一段时间后,上传接口出现问题,数据库用的MySQL5 ...

  2. c# .net 4.5.2 asp.net mvc 使用hangfire

    一定要有hangfire数据库,否则hangfire会报错. (obStorage.Current property value has not been initialized. You must ...

  3. nginx 移动端和pc端自动跳转

    场景 项 域名 描述 pc端 www.one.com 用于pc端访问官网 移动端 m.one.com 用于移动端访问 现在的需求是这样,在pc端访问www.one.com和m.one.com都跳转到w ...

  4. shell脚本检查是否存在tun0虚拟网卡,若不不存在服务器更改port,并重启服务器,客户端修改port,并重新启动客户端

    openvp 客户端 /home 目录下各脚本文件名 [root@jira home]# ls openvpn_server_restart.sh openvpn_tunnel_monitor.sh ...

  5. elastalert 用import属性来组织,引入配置

    对应rule的一些公用规则,可以放到一个或者多个头文件中.主的rule yaml文件引入即可.文件名随意,最好别用yaml后缀,要不会被当做一个rule.另外import在rule文件中只能出现一次. ...

  6. Ubuntu下局域网快速分享文件

    本地主机名:zhang 本地环境:Ubuntu 18.04.3 工作中经常需要在多个机器上互传文件,本文分享一种便捷的方法,仅供应急使用. 利用了mdns和python3内置的httpServer.( ...

  7. [bug]——vue 组件状态外置引发的一个 bug

    背景 在编写 .vue 组件时,可以将状态外置来获取一些额外的好处,譬如有这么一个组件(global-components.vue): <template> <div> < ...

  8. Redis中3种特殊的数据类型(BitMap、Geo和HyperLogLog)

    前言 Reids 在 Web 应用的开发中使用非常广泛,几乎所有的后端技术都会有涉及到 Redis 的使用.Redis 种除了常见的字符串 String.字典 Hash.列表 List.集合 Set. ...

  9. VirtualBox 配置 CentOS7网卡信息

    在实际配置虚拟机的过程中,网络配置时候一个很繁琐的过程,经常一个点没注意到,就访问不了了.在此,做一个简单的教程以供后续使用时可以参考! 方法一: 使用NAT网络 1. 选择网卡 安装centos7的 ...

  10. [转帖]Shell运维手册

    shell实例手册    https://github.com/liquanzhou/ops_doc   0 说明{       手册制作: 雪松   更新日期: 2018-09-11       欢 ...