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 11-st day it will belong to the 55-th kid,
  • after the 22-nd day it will belong to the 33-rd kid,
  • after the 33-rd day it will belong to the 22-nd kid,
  • after the 44-th day it will belong to the 11-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

Copy
  1. 6
  2. 5
  3. 1 2 3 4 5
  4. 3
  5. 2 3 1
  6. 6
  7. 4 6 2 1 5 3
  8. 1
  9. 1
  10. 4
  11. 3 4 1 2
  12. 5
  13. 5 1 2 4 3
output

Copy
  1. 1 1 1 1 1
  2. 3 3 3
  3. 2 3 3 2 1 3
  4. 1
  5. 2 2 2 2
  6. 4 4 4 1 4

  1.  
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <set>
  6. #include <queue>
  7. #include <map>
  8. #include <sstream>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <numeric>
  12. #include <cmath>
  13. #include <unordered_set>
  14. #include <unordered_map>
  15. #define ll long long
  16. using namespace std;
  17. int dir[][] = { {,},{,-},{-,},{,} };
  18. vector<int> p,help,res;
  19. int cnt;
  20. void dfs(int start, int pos)
  21. {
  22. help.push_back(pos);
  23. cnt++;
  24. int t = p[pos];
  25. p[pos] = ;
  26. if (start == t)
  27. return;
  28. dfs(start, t);
  29. }
  30.  
  31. int main()
  32. {
  33. int q;
  34. cin >> q;
  35. while (q--)
  36. {
  37. int n;
  38. cin >> n;
  39. p.resize(n+);
  40. res.resize(n + );
  41.  
  42. for (int i = ; i <= n; i++)
  43. cin >> p[i];
  44. for (int i = ; i <= n; i++)
  45. {
  46. help.clear();
  47. cnt = ;
  48. if (p[i] == )
  49. continue;
  50. dfs(i, i);
  51. for (int j = ; j < help.size(); j++)
  52. {
  53. res[help[j]] = cnt;
  54. }
  55. }
  56. for (int i = ; i < res.size(); i++)
  57. cout << res[i] << " ";
  58. cout << endl;
  59. }
  60. //system("pause");
  61. return ;
  62. }

Books Exchange (hard version)的更多相关文章

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

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

  2. Books Exchange (easy version)   CodeForces - 1249B2

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

  3. Codeforces--Books Exchange (hard version)

    题目链接http://codeforces.com/contest/1249/problem/B2 .并查集思想,将数分成多个集合,每个集合的大小就是一轮的所需天数. Map[i]存储数据. flag ...

  4. cf 595 补题

    1.B2   Books Exchange (hard version) 题意:有n(1~n)个孩子看书,定义一个数组,记录了每个孩子看完

  5. Codeforces Round #595 (Div. 3)

    A - Yet Another Dividing into Teams 题意:n个不同数,分尽可能少的组,要求组内没有两个人的差恰为1. 题解:奇偶分组. int a[200005]; void te ...

  6. dfs(找环)

    https://codeforces.com/problemset/problem/1249/B2 B2. Books Exchange (hard version) time limit per t ...

  7. CodeForces1249B1/B2-Books Exchange-dfs-一般搜索+记忆化搜索

    一般搜索 注意:一般定义成void Books Exchange (easy version)  CodeForces - 1249B2 The only difference between eas ...

  8. Financial Information Exchange (FIX) Protocol Interview Questions Answers[z]

    What do you mean by Warrant?Warrant is a financial product which gives right to holder to Buy or Sel ...

  9. CF-595

    题目传送门 A .Yet Another Dividing into Teams sol:原先是用比较复杂的方法来解的,后来学弟看了一眼,发现不是1就是2,当出现两个人水平相差为1就分成两组,1组全是 ...

随机推荐

  1. kindle怎么导入电子书

    参考网址:https://jingyan.baidu.com/article/59a015e342a165f795886545.html

  2. OCM 12c | OCM 12c Update | OCM 11g (Retiring Dec 31, 2019) | OCM 11g考试延期至2020.04.30

     OCM 全球考试安排时间表 View A Worldwide OCM Schedule Oracle Database 12c Certified Master Exam (OCM) OCM 12c ...

  3. Django内置的中间件

    内置中间件 1. django.middleware.gzip.GZipMiddleware:相应数据进行压缩.如果内容长度少于200个长度,那么就不会压缩. 在settings.py文件中配置MID ...

  4. 群晖DSM修改ssh权限实现免密码登陆

    问题 使用ssh-id-copy正确上传公钥后依然无法免密码登陆 原因 群晖DSM中.ssh文件夹权限不当 解决 赋予正确权限 admin@DiskStation:/var/services/home ...

  5. [POI2015]PUS [线段树优化建图]

    problem 线段树优化建图,拓扑,没了. #include <bits/stdc++.h> #define ls(x) ch[x][0] #define rs(x) ch[x][1] ...

  6. 使用ESLint+Prettier来统一前端代码风格

    Prettier 简单使用 ESLint 与 Prettier配合使用 首先肯定是需要安装 prettier ,并且你的项目中已经使用了 ESLint ,有 eslintrc.js 配置文件. npm ...

  7. Hibernate 和Mybatis的区别

    Hibernate 和Mybatis的区别   1.hibernate 入门门槛高,是一个标准的ORM框架(对象关系映射),不需要程序写sql,sql语句自动生成,对sql语句进行优化.修改比较困难. ...

  8. ASP.NET MVC 简介(附VS2019和VSCode版示例)

    MVC可以理解为一种思想,应用在web应用程序的架构上. ASP.NET MVC的核心类是实现了IHttpHandler接口的MVCHandler,它的底层仍然是HttpHandler.HttpReq ...

  9. Wannafly Camp 2020 Day 1F 乘法 - 字符串

    一开始想根据单调性双指针 后来血了才想起来负负得正 于是暴力二分答案即可 #include <bits/stdc++.h> using namespace std; #define int ...

  10. PHP实现导出Excel文件

    实现代码: private function exportExcel($params) { $arr = $this->getExportData($params); // 获取需要导出的信息( ...