C - 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 11to 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] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 66-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 33-th kid, the 2-nd kid will belong to the 2-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 i from 1 to nn.

Consider the following example: 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≤1000) — the number of queries. Then qq queries follow.

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

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

Output

For each query, print the answer on it: nn integers a1,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 

 这一题的题意很好理解,就是求经过多少次,那个原来属于自己的书, 再跑到自己手上。可以用记忆化写,如果构成一个环,则这个环上每一个点的个数答案都一样。所以就省了重复的搜索空间。记忆化搜索。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define ll long long
using namespace std;
long long t,n,x1,x2,s;
long long a[1000001];
long long b[1000001],c[1000001];
void dfs(int i,int j)
{
    s++;
    if(a[i]==j)
    {
        c[i]=s;
        return;
    }
    else
    {
        dfs(a[i],j);
        c[i]=s;
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        s=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
           // b[i]=i;
            c[i]=0;
        }
        for(int i=1; i<=n; i++)
        {
            if(c[i]==0)
            {
                s=0;
                dfs(i,i);
            }
        }
        for(int i=1; i<n; i++)
        printf("%d ",c[i]);
        printf("%d\n",c[n]);
    }
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值