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 1-st day it will belong to the 5-th kid,
  • after the 2-nd day it will belong to the 3-rd kid,
  • after the 3-rd day it will belong to the 2-nd kid,
  • after the 4-th day it will belong to the 1-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 解题思路:数组值与数组下标相等时即为解,需要注意的是,return结果。 AC代码:
#include <iostream>
using namespace std;
const int maxn=;
int a[maxn];
int cnt=;
int search(int x,int s)
{
if(a[x]==s)
return cnt;
else
{
cnt++;
return search(a[x],s);
}
}
int main()
{
int q,n;
cin>>q;
while(q--)
{ cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
int res;
for(int i=;i<=n;i++)
{
cnt=;
res=search(i,i);
cout<<res<<" ";
}
cout<<endl;
}
return ;
}

第二种使用数组存储结果:

#include <iostream>
#include <cstring>
using namespace std;
const int maxn=;
int a[maxn];
int res[maxn];
int cnt=;
void search(int x,int s)
{
if(a[x]==s)
res[s]=cnt;
else
{
cnt++;
search(a[x],s);
}
}
int main()
{
int q,n;
cin>>q;
while(q--)
{
memset(a,,sizeof a);
memset(res,,sizeof res);
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
{
cnt=;
search(i,i);
}
for(int i=;i<=n;i++)
cout<<res[i]<<" ";
cout<<endl;
}
return ;
}
 

Books Exchange (easy version)   CodeForces - 1249B2的更多相关文章

  1. CodeForces - 1214D B2. Books Exchange (hard version)

    题目链接:http://codeforces.com/problemset/problem/1249/B2 思路:用并查集模拟链表,把关系串联起来,如果成环,则满足题意.之后再用并查集合并一个链,一个 ...

  2. Books Exchange (hard version)

    The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...

  3. Into Blocks (easy version)

    G1 - Into Blocks (easy version) 参考:Codeforces Round #584 - Dasha Code Championship - Elimination Rou ...

  4. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

  5. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  6. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  7. Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)

    F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...

  8. Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题

    B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...

  9. Codeforces 1077F1 Pictures with Kittens (easy version)(DP)

    题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...

随机推荐

  1. spring定时任务的配置式与注解式

    在定时任务配置文件中添加内容: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&q ...

  2. Javascritp 数据结构和二叉树算法

    1,所有圆圈都是一个节点,里面的数字就是节点的值.8上面没有父节点,那么8就是根节点,而4,7,13没有子节点了,称之为叶子结点.其他的称之为:中间结点. 2,8节点是3和10的父节点,3是8的左孩子 ...

  3. 【Java-算法】 计算十六进制校验位

    如何计算16进制校验位?校验的实质是16进制取和模256的值. eg. 十六进制串:0A0B0C0D0E0F 的校验位是: 4B (HEX)计算过程: 十六进制转十进制 0A (HEX)= 10(DE ...

  4. which/whereis

    which 查找二进制命令,按环境变量PATH路径查找 whereis 查找二进制命令,按环境变量PATH路径查找 查询命令的安装路径,配置文件路径

  5. Java进阶知识15 Spring的基础配置详解

    1.SSH各个的职责 Struts2:是web框架(管理jsp.action.actionform等).Hibernate:是ORM框架,处于持久层.Spring:是一个容器框架,用于配置bean,并 ...

  6. [Luogu] 计数

    https://www.luogu.org/problemnew/show/P3130 #include <cstdio> #include <iostream> using ...

  7. EM算法 小结

    猴子吃果冻 博客园 首页 新随笔 联系 管理 订阅 随笔- 35  文章- 0  评论- 3  4-EM算法原理及利用EM求解GMM参数过程   1.极大似然估计 原理:假设在一个罐子中放着许多白球和 ...

  8. 转:Jmeter文件上传、下载

    一.上传/下载的过程.        上传的过程就是你把你本地的文件,扔到服务器上的这么一个过程.        下载呢,就是把服务器上的文件拿过来,然后存到你本地的这么一个过程.        总结 ...

  9. 原生javascript封装动画库

    ****转载自自己发表于牛人部落专栏的文章**** 一.前言 本文记录了自己利用原生javascript构建自己的动画库的过程,在不断改进的过程中,实现以下动画效果: 针对同一个dom元素上相继发生的 ...

  10. window 定时任务小项目,每秒钟,每分钟访问url

    个人百度网盘中 链接: https://pan.baidu.com/s/15sK2H-OtrAkAYcXjsxwihA 提取码: awv3 复制这段内容后打开百度网盘手机App,操作更方便哦