一般搜索

注意:一般定义成void

Books Exchange (easy version)   CodeForces - 1249B2

The only difference between easy and hard versions is constraints.

There are nn kids, each of them is reading a unique book. At the end of any day, the ii-th kid will give his book to the pipi-th kid (in case of i=pii=pi the kid will give his book to himself). It is guaranteed that all values of pipi are distinct integers from 11 to nn (i.e. pp is a permutation). The sequence pp doesn't change from day to day, it is fixed.

For example, if n=6n=6 and p=[4,6,1,3,5,2]p=[4,6,1,3,5,2] then at the end of the first day the book of the 11-st kid will belong to the 44-th kid, the 22-nd kid will belong to the 66-th kid and so on. At the end of the second day the book of the 11-st kid will belong to the 33-th kid, the 22-nd kid will belong to the 22-th kid and so on.

Your task is to determine the number of the day the book of the ii-th child is returned back to him for the first time for every ii from 11 to nn.

Consider the following example: p=[5,1,2,4,3]p=[5,1,2,4,3]. The book of the 11-st kid will be passed to the following kids:

  • after the 11-st day it will belong to the 55-th kid,
  • after the 22-nd day it will belong to the 33-rd kid,
  • after the 33-rd day it will belong to the 22-nd kid,
  • after the 44-th day it will belong to the 11-st kid.

So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤10001≤q≤1000) — the number of queries. Then qq queries follow.

The first line of the query contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of kids in the query. The second line of the query contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n, all pipi are distinct, i.e. pp is a permutation), where pipi is the kid which will get the book of the ii-th kid.

It is guaranteed that ∑n≤2⋅105∑n≤2⋅105 (sum of nn over all queries does not exceed 2⋅1052⋅105).

Output

For each query, print the answer on it: nn integers a1,a2,…,ana1,a2,…,an, where aiai is the number of the day the book of the ii-th child is returned back to him for the first time in this query.

Example

Input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
Output
1 1 1 1 1
3 3 3
2 3 3 2 1 3
1
2 2 2 2
4 4 4 1 4 题意:
给出t组数,每组给出n,表示接下来有n本书,a[i]=k,表示第i个人需要把手上的书给第k个人。
问:编号为i的书回到第i个人手里需要多少次。 看第二组样例输入2 3 1,输出3 3 3。
  • sum1=0,
        第1个人把手上的书给第2个人,sum++,第二个人把书给第3个人,sum++,第三个人把书给第1个人,sum++,所以sum1=3;
  • sum2=0,
   第2个人把手上的书给第3个人,sum++,第3个人把书给第1个人,sum++,第1个人把书给第2个人,sum++,所以sum2=3;
  • sum3=0,
   第3个人把手上的书给第1个人,sum++,第1个人把书给第2个人,sum++,第2个人把书给第3个人,sum++,所以sum3=3;

思路:

开一个for循环,然后dfs(i,i,1)
i:第i个人
i:第i个人所对应下次把书传到a[i]手上去
1:代表书转手次数=1,第一次书在自己手上,所以次数为1,以后每次搜索次数++ 开始进行搜索
void dfs(int x,int k,int t)//传入下标x,下标k,转手次数t
{
if(w)
return;//剪枝
if(x==a[k])//此时表示第x个人找到了对应a[k]=x的书
{
w=t;//定义w为全局变量,w=t,记录转手次数
return ;
}
dfs(x,a[k],t+);//否则还未找到,继续进行搜索
}

代码如下:
 #include<stdio.h>
int a[],w; void dfs(int x,int k,int t)
{
if(w)
return;
if(x==a[k])
{
w=t;
return ;
}
dfs(x,a[k],t+);
} int main()
{
int t,m;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
w=;
dfs(i,i,);
printf("%d ",w);
}
printf("\n");
}
return ;
}

记忆化搜索

注意:一般定义成int,需要有返回值,表示之前已经搜到过的值,无需再搜

Books Exchange (hard version)  CodeForces - 1249B2

The only difference between easy and hard versions is constraints.

There are nn kids, each of them is reading a unique book. At the end of any day, the ii-th kid will give his book to the pipi-th kid (in case of i=pii=pi the kid will give his book to himself). It is guaranteed that all values of pipi are distinct integers from 11 to nn (i.e. pp is a permutation). The sequence pp doesn't change from day to day, it is fixed.

For example, if n=6n=6 and p=[4,6,1,3,5,2]p=[4,6,1,3,5,2] then at the end of the first day the book of the 11-st kid will belong to the 44-th kid, the 22-nd kid will belong to the 66-th kid and so on. At the end of the second day the book of the 11-st kid will belong to the 33-th kid, the 22-nd kid will belong to the 22-th kid and so on.

Your task is to determine the number of the day the book of the ii-th child is returned back to him for the first time for every ii from 11 to nn.

Consider the following example: p=[5,1,2,4,3]p=[5,1,2,4,3]. The book of the 11-st kid will be passed to the following kids:

  • after the 11-st day it will belong to the 55-th kid,
  • after the 22-nd day it will belong to the 33-rd kid,
  • after the 33-rd day it will belong to the 22-nd kid,
  • after the 44-th day it will belong to the 11-st kid.

So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤10001≤q≤1000) — the number of queries. Then qq queries follow.

The first line of the query contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of kids in the query. The second line of the query contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n, all pipi are distinct, i.e. pp is a permutation), where pipi is the kid which will get the book of the ii-th kid.

It is guaranteed that ∑n≤2⋅105∑n≤2⋅105 (sum of nn over all queries does not exceed 2⋅1052⋅105).

Output

For each query, print the answer on it: nn integers a1,a2,…,ana1,a2,…,an, where aiai is the number of the day the book of the ii-th child is returned back to him for the first time in this query.

Example

Input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
Output
1 1 1 1 1
3 3 3
2 3 3 2 1 3
1
2 2 2 2
4 4 4 1 4 思路:
由于数据变大了,所以按照上面的方法进行深搜会超时,所以需要用到记忆化搜素。
开一个book数组,book[x]=w,表示书回到第x个人手上需要转手w次。
假设输入2 3 1,a[i]=k的书回到第k个人手中需要t次的话,那么在转手过程中所经过的所有点k回到第k个人手中也需要t次,
所以开一个b数组,把所经过的所有k全部保存下来,for循环往下走走到该点就不需要再搜,直接输出即可
利用book数组,把b数组里面存的所有数(有p个)通过循环赋值为k,即book[ b[i] ]=w。

代码如下:

 #include<stdio.h>
#include<string.h>
const int N=2e5+;
int a[N],w,b[N],p,book[N]; void dfs(int x,int k,int t)
{
if(x==a[k])
{
w=t;
book[x]=w;
return;
// return book[x]=t;
}
b[p++]=a[k];//把走过的点全部存起来,下次碰到不用再走就不会超时
dfs(x,a[k],t+);
} int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(book,,sizeof(book));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
p=;
if(i!=)
printf(" ");
if(book[i])
{
printf("%d",book[i]);
continue;
}
// w=0;
dfs(i,i,);
for(int j=;j<p;j++)
book[b[j]]=w;
printf("%d",w);
}
printf("\n");
}
return ;
}

CodeForces1249B1/B2-Books Exchange-dfs-一般搜索+记忆化搜索的更多相关文章

  1. skiing(搜索+记忆化搜索)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  2. 【蓝桥杯真题】地宫取宝(搜索->记忆化搜索详解)

    链接 [蓝桥杯][2014年第五届真题]地宫取宝 题目描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被 ...

  3. P4363 [九省联考2018]一双木棋chess(对抗搜索+记忆化搜索)

    传送门 这对抗搜索是个啥玩意儿…… 首先可以发现每一行的棋子数都不小于下一行,且局面可由每一行的棋子数唯一表示,那么用一个m+1进制数来表示当前局面,用longlong存,开map记忆化搜索 然后时间 ...

  4. hdu1078 dfs+dp(记忆化搜索)搜索一条递增路径,路径和最大,起点是(0,0)

    #include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...

  5. 数位dp/记忆化搜索

    一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an  ...

  6. 蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索

    问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...

  7. 【noip 2009】 乌龟棋 记忆化搜索&动规

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...

  8. hdu 1428(很好的一道题,最短路+记忆化搜索)

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. POJ1088滑雪(记忆化搜索)

    就是用DP,DP[i][j]是在这个(i,j)位置作为起点的最长长度. 因为可能会超时,DP的话每次就是记录,然后就不用回溯了. 很简单的DFS里面的记忆化搜索. #include <stdio ...

随机推荐

  1. Linux环境安装PHP链接SQLserver2008

    网上很多介绍,但是跟着做总是有问题,找到一个大神的,没出现过问题: 1.首先需要编译安装FreeTDS 1.1.安装 说明:一定要从官网下载最新的版本FreeTDS-0.95 ftp://ftp.fr ...

  2. 微信小程序学习笔记(一)--创建微信小程序

    一.创建小程序 1.申请帐号.安装及创建小程序,请参照官方文档里面的操作 https://developers.weixin.qq.com/miniprogram/dev/. 小程序在创建的时候会要求 ...

  3. hibernate使用手写sql以及对结果list的处理

    Session sees=simpleDAO.getSessionFactory().openSession(); String sql = "select * from fhcb_08_t ...

  4. linux下根据根据进程号查端口、根据端口号查进程号汇总,以及netstat的相关资料(工作中匮乏的知识)

    根据端口查进程: lsof -i:port netstat -nap | grep port 根据进程号查端口: lsof -i|grep pid netstat -nap | grep pid 根据 ...

  5. Linux下使用java获取cpu、内存使用率

    原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html 思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类 ...

  6. MySQL数据库(一)—— 数据库介绍、MySQL安装、基础SQL语句

    数据库介绍.MySQL安装.基础SQL语句 一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 ...

  7. Error: EBUSY: resource busy or locked, symlink "xxx" 的解决方法

    上面是报错信息. 解决方法:当我们在npm i 安装依赖的时候,会遇到这个Error: EBUSY: resource busy or locked, symlink....的问题.解决的 办法就是关 ...

  8. 高级UI晋升之常用View(三)中篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从ViewPager来介绍常用View:文章目录 一.简介 二.基本使用 ...

  9. C# WinfForm 控件之dev报表 XtraReport (八)动态生成报表

    功能说明:生成一个报表文件DV1,保存到本地AA.rep,再重新加载这个文件到DV2 1.布局如下图: panel1 上有三个button panel2上有个documentView dv1 pane ...

  10. 一.Python特点

    python第一节 简介 Python介绍 什么样的语言? 解释性语言:开发中没有编译的环节 交互式语言:在命令提示行执行python引擎,直接执行代码 面向对象语言:支持面向对象 优点 a.易学 b ...