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. SendMessage到底是如何工作的?

    以下内容摘自<<Windows核心编程>>: 概要: SendMessage对于在同一个线程中调用的话,直接调用的是当前线程所属窗口的窗口过程函数(WndProc);如果是跨线 ...

  2. git命令行提交流程

    一.顺利提交无冲突情况(diff->add->fetch->pull->commit->push) 1.git  status 查看状态 2. git diff head ...

  3. 中介者模式(Mediator)---行为型

    1 基础知识 定义:用一个中介对象来封装一系列的对象交互.中介者使得各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 本质:封装交互 使用场景:(1)如果一组对象之间的 ...

  4. 小米 oj 找到第N个数字||

    Mycode: #include<iostream> #include<stdio.h> #include<string.h> using namespace st ...

  5. JavaScript闭包应用场合——控制前端接口轮训

    很多人都知道JavaScript的闭包,也知道大致是一个什么意思,但是对于闭包的应用场合不是很清楚 最近在改造项目的过程之中修改前端接口轮训方式的时候用到了闭包驱动setTimeout来实现一个类似定 ...

  6. JavaWeb_(Hibernate框架)Hibernate论坛项目中一对多案例

    基于SSH论坛小型项目 传送门 用户和发帖进行举例 一对多关系:一个用户可以发表多个帖子 一对一关系:一个帖子属于一个用户发布 创建数据库用户user表 CREATE TABLE `hforum`.` ...

  7. mybatis之<trim

    1.<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=&quo ...

  8. PHP遍历目录下的文件夹和文件 以及遍历文件下内容

    1.遍历目录下的文件夹和文件: public function bianli1($dir) { $files = array(); if($head = opendir($dir)) { while( ...

  9. 中间件 | kafka简介、使用场景、设计原理、主要配置及集群搭建

    开源Java学习 公众号 一.入门 1.简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性 ...

  10. AndroidStudio NDK环境3种搭建方式

    AndroidStudio NDK环境3种搭建方式     转载. https://blog.csdn.net/zhang_jun_ling/article/details/85621427 一.前言 ...