题目描述

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

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

Solution

总体上来说不是一个特别难想的DP,很容易发现的是,这个题目中可能一定会出现环,那么环上的点的ans都是一样的,只需要单独再算其他的点就可以了。

那么我们先拓扑排序找环,然后直接暴力枚举就可以了。易知的是每个点最多被访问一次,所以这个复杂度是O(n)的。

Code

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define re register
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(arr) memset(arr, 0, sizeof(arr))
const int inf = 0x3f3f3f3f;
int n,a[120001],nxt[120001],ans[120001],cnt,vis[120001],tt,flag,rd[120001];
inline int read()
{
int x=0,c=1;
char ch=' ';
while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
while(ch=='-') c*=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
return x*c;
}
inline void search(int x)
{
vis[x]=1;
rd[nxt[x]]--;
if(!rd[nxt[x]]) search(nxt[x]);
}
inline int huan(int x,int depth)
{
ans[x]=depth;
if(ans[nxt[x]]) return depth;
return ans[x]=huan(nxt[x],depth+1);
}
inline int solve(int x)
{
if(ans[x]) return ans[x];
else return ans[x]=solve(nxt[x])+1;
}
int main()
{
//freopen("date.in","r",stdin);
n=read();
for(re int i=1;i<=n;i++){
nxt[i]=read();rd[nxt[i]]++;
}
for(re int i=1;i<=n;i++){
if(rd[i]==0&&!vis[i])
search(i);
}
for(re int i=1;i<=n;i++){
if(rd[i]!=0&&!ans[i])
huan(i,1);
}
for(re int i=1;i<=n;i++){
if(!rd[i]&&!ans[i])
solve(i);
}
for(re int i=1;i<=n;i++)
printf("%d\n", ans[i]);
return 0;
}

最新文章

  1. windbg运行
  2. 【记忆化搜索】bzoj1652 [Usaco2006 Feb]Treats for the Cows
  3. javascript 常用技巧
  4. Nginx使用的php-fpm的两种进程管理方式及优化(转)
  5. 关于LED 流水灯的软件调试方法(非开发板调试)
  6. mysql修改root密码和设置权限
  7. node中的get请求和post请求的不同操作【node学习第五篇】
  8. UML类图几种关系总结
  9. electron打包vue项目
  10. centos7编译安装zabbix(附带编译安装lnmp)
  11. Socket网络编程--小小网盘程序(2)
  12. phpStudy安装教程
  13. CF1063A 【Oh Those Palindromes】
  14. can 驱动
  15. 简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
  16. MySQL 数据库--权限管理
  17. 目标检测-Faster R-CNN
  18. vs code 配置spring boot开发环境
  19. 面试题 数据库sql
  20. 自己做的roguelike+恶魔城游戏《魔塔猎人》已发布。

热门文章

  1. libevent(1)
  2. Mac下安装apk(命令形式)
  3. angularjs中的$destroy和$timeout
  4. jpa单向一对多关联映射
  5. nodemailer发送邮件各个服务器接口
  6. 《挑战程序设计竞赛》2.4 数据结构-并查集 POJ1182 2236 1703 AOJ2170
  7. java 统计文件注释个数
  8. tarball源码安装
  9. Django基础一
  10. Delphi 正则表达式语法(1): 关于大小写与中文