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. Navicat Premium 12连接mysql-8.0.15-winx64 出现2059异常

    错误

  2. (二)线程Thread中的方法详解

    1.start() start()方法的作用讲得直白点就是通知"线程规划器",此线程可以运行了,正在等待CPU调用线程对象得run()方法,产生一个异步执行的效果.通过start( ...

  3. json注解及序列化

    一.json框架 市面上的json框架常用的有 jackson.gson.fastjson.大家比较推崇的是fastjson,但是springmvc默认集成的是 jackson. 在一个项目中建议一个 ...

  4. 给程序添加git commit信息

    遇到了一个客户程序出问题,自己这边始终无法重现的bug.为了检查问题,查到了一个添加git的commit信息到程序中的方法,感觉对程序版本控制十分好用. 一,项目中添加如下文件 文件结构: GitVe ...

  5. XHTML测试题

    1.XHTML 指的是? A.EXtra Hyperlinks and Text Markup Language B.EXtensible HyperText Marking Language C.E ...

  6. BZOJ 2440 [中山市选2011]完全平方数 二分+容斥

    直接筛$\mu$?+爆算?再不行筛素数再筛个数?但不就是$\mu^2$的前缀和吗? 放...怕不是数论白学了$qwq$ 思路:二分+容斥 提交:两次(康了题解) 题解: 首先答案满足二分性质(递增), ...

  7. Unknown property 'mybatis-plus'

    包名和配置文件路径必须一样 并且

  8. linux系统编程--守护进程,会话,进程组,终端

    终端: 在UNIX系统中,用户通过终端登录系统后得到一个Shell进程,这个终端成为Shell进程的控制终端(Controlling Terminal), 进程中,控制终端是保存在PCB中的信息,而f ...

  9. window上git bash运行错误记录

    错误现象:每次启动git bash报出如下错误gitbash  0 [main] bash 11928 fork: child -1 - CreateProcessW failed for 'D:\P ...

  10. DataTable转List,DataTable转为Model对象帮助类

    DataTable转List,DataTable转为Model对象帮助类 public class ModelConvertHelper<T> where T : new() { publ ...