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. Google Maps API3 之 Hello World

    <html xmlns="http://www.w3.org/1999/xhtml">     <head>         <meta http-e ...

  2. HDU 6051 - If the starlight never fade | 2017 Multi-University Training Contest 2

    /* HDU 6051 - If the starlight never fade [ 原根,欧拉函数 ] | 2017 Multi-University Training Contest 2 题意: ...

  3. JSP大文件分片上传

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  4. webpack给目录起别名

    1. 配置文件目录: build>webpack.base.config.js: resolve: { alias: { '@': resolve('src'), 'styles': resol ...

  5. PCIe/NVMe Soft Protocol Analyzer - 惊天发现

    最近在看一些协议方面的东西,刚刚接触很多东西不懂.为了尽快熟悉,打算搭建一套系统,包括上层主机和下层SSD. 既然是协议,那就需要硬啃spec.但是俺是懒人一个,想找个便捷方式快速熟悉.这就想到了协议 ...

  6. 顺序表应用1:多余元素删除之移位算法(SDUT 3324)

    Problem Description 一个长度不超过10000数据的顺序表,可能存在着一些值相同的"多余"数据元素(类型为整型),编写一个程序将"多余"的数据 ...

  7. 基于CentOS 7下最小化安装的操作系统搭建Zabbix3.0环境

    环境说明 系统版本:CentOS Linux release 7.3.1611 (Core) 内核版本:3.10.0-514.el7.x86_64 Httpd版本:Apache/2.4.6 (Cent ...

  8. leetcode题目234.回文链表(快慢指针+辅助空间-简单)

    题目描述: 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O( ...

  9. IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目(一)

    新人接触springboot,IDE使用的是IntelliJ IDEA 2017.3 ,自己摸索了很久,现在自己整理一下,里面有些操作我自己也不懂是为什么这样,只是模仿公司现有的项目,自己搭建了一个简 ...

  10. 笔记一(固件、BIOS、UEFI)

    1.固件 固件一般是指保存在ROM中的程序和数据,通过固件操作系统按照标准的设备驱动实现特定机器的运行. 简单来讲,固件就是固化在ROM的软件,当然也可以通过特定的工具进行升级. MP3.MP4.手机 ...