https://codeforces.com/problemset/problem/1249/B2

B2. Books Exchange (hard version)
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 qqqueries 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. //#include <bits/stdc++.h>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <cstdio>
  10. #include <string>
  11. #include <cstring>
  12. #include <stdio.h>
  13. #include <queue>
  14. #include <stack>;
  15. #include <map>
  16. #include <set>
  17. #include <string.h>
  18. #include <vector>
  19. #define ME(x , y) memset(x , y , sizeof(x))
  20. #define SF(n) scanf("%d" , &n)
  21. #define rep(i , n) for(int i = 0 ; i < n ; i ++)
  22. #define INF 0x3f3f3f3f
  23. #define mod 998244353
  24. #define PI acos(-1)
  25. using namespace std;
  26. typedef long long ll ;
  27. int vis[] , a[];
  28. int sum ;
  29.  
  30. void dfs(int x , int i)
  31. {
  32. sum++ ;
  33. if(x == i)
  34. {
  35. vis[x] = sum ;
  36. return ;
  37. }
  38. else
  39. {
  40. x = a[x];
  41. dfs(x , i);
  42. vis[x] = sum ;
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48.  
  49. int t ;
  50. scanf("%d" , &t);
  51. while(t--)
  52. {
  53. int n ;
  54. scanf("%d" , &n);
  55. for(int i = ; i <= n ; i++)
  56. {
  57. scanf("%d" , &a[i]);
  58. vis[i] = ;
  59. }
  60.  
  61. for(int i = ; i <= n ; i++)
  62. {
  63. sum = ;
  64. if(!vis[i])
  65. dfs(a[i] , i);
  66. }
  67.  
  68. for(int i = ; i < n ; i++)
  69. {
  70. cout << vis[i] << " ";
  71. }
  72. cout << vis[n] << endl ;
  73. }
  74.  
  75. return ;
  76. }
  1. //#include <bits/stdc++.h>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <cstdio>
  10. #include <string>
  11. #include <cstring>
  12. #include <stdio.h>
  13. #include <queue>
  14. #include <stack>;
  15. #include <map>
  16. #include <set>
  17. #include <string.h>
  18. #include <vector>
  19. #define ME(x , y) memset(x , y , sizeof(x))
  20. #define SF(n) scanf("%d" , &n)
  21. #define rep(i , n) for(int i = 0 ; i < n ; i ++)
  22. #define INF 0x3f3f3f3f
  23. #define mod 998244353
  24. #define PI acos(-1)
  25. using namespace std;
  26. typedef long long ll ;
  27. int vis[] , a[];
  28.  
  29. int main()
  30. {
  31.  
  32. int t ;
  33. scanf("%d" , &t);
  34. while(t--)
  35. {
  36. int n ;
  37. scanf("%d" , &n);
  38. for(int i = ; i <= n ; i++)
  39. {
  40. scanf("%d" , &a[i]);
  41. vis[i] = ;
  42. }
  43. queue<int>q;
  44. for(int i = ; i <= n ; i++)
  45. {
  46. if(vis[i]) continue ;
  47. int pos = i ;
  48. do
  49. {
  50. q.push(pos);
  51. pos = a[pos];
  52. }while(pos!=i);
  53. int cnt = q.size();
  54. while(!q.empty())
  55. {
  56. vis[q.front()] = cnt ;
  57. q.pop();
  58. }
  59.  
  60. }
  61. for(int i = ; i < n ; i++)
  62. {
  63. cout << vis[i] << " ";
  64. }
  65. cout << vis[n] << endl ;
  66. }
  67.  
  68. return ;
  69. }

dfs(找环)的更多相关文章

  1. # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)

    「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...

  2. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

  3. CodeForces - 103B(思维+dfs找环)

    题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...

  4. CodeForces 711D Directed Roads (DFS找环+组合数)

    <题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...

  5. 与图论的邂逅06:dfs找环

    当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...

  6. HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)

    求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...

  7. UVaLive 6950 && Gym 100299K Digraphs (DFS找环或者是找最长链)

    题意:有n个只包含两个字母的字符串, 要求构造一个m*m的字母矩阵, 使得矩阵的每行每列都不包含所给的字符串, m要尽量大, 如果大于20的话构造20*20的矩阵就行了. 析:开始吧,并没有读对题意, ...

  8. [NOI2008]假面舞会——数论+dfs找环

    原题戳这里 思路 分三种情况讨论: 1.有环 那显然是对于环长取个\(gcd\) 2.有类环 也就是这种情况 1→2→3→4→5→6→7,1→8→9→7 假设第一条链的长度为\(l_1\),第二条为\ ...

  9. [蓝桥杯2018初赛]小朋友崇拜圈(dfs找环)

    传送门 思路: 题意大意:n条有向边,找出最大环. 我们发现,如果一个小朋友没有被任何人崇拜,那么他一定不位于环中.为此我们可以设置一个indug数组预处理.如果2被崇拜了那么indug[2]就加加, ...

  10. New Reform---cf659E(dfs找环)

    题目链接:http://codeforces.com/problemset/problem/659/E 给你n个点,m条双向边,然后让你把这些边变成有向边,使得最后的图中入度为0的点的个数最少,求最少 ...

随机推荐

  1. Java_环境变量

    介绍 第一步:下载JDK 第二步:搭建环境,双击JDK安装程序 第三步:配置环境变量 第四步:检查JDK安装是否成功 介绍: .java 源文件 我们所编写的代码都在这个文件中 .class 字节码文 ...

  2. unittest----skip(跳过用例)

    我们在执行测试用例时,有时有些用例时不需要执行的,这时就需要用到unittest给我们提供的跳过用例的方法 @unittest.skip(reason):强制跳过,不需要判断条件.reason是跳过原 ...

  3. 【ZJOI2008】树的统计

    题目 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: ...

  4. 使用Spring.Net进行Webservice开发&发布遇到的问题

    发布遇到的问题1: HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理. 最终解决时IIS的设置情况: 1.应用程序池的高级设置中 启用32 ...

  5. 使用layui iframe弹层,各弹层之前的传值问题

    最近做一个后台管理系统,用到的layui,主要是使用它的弹层,但是各个弹层之前的传值经常容易搞晕,写个个博客记录一下,方便自己,也方便别人, 首先我的页面已经嵌套了好几个iframe页面了,嵌套了三个 ...

  6. 第八周作业—N42-虚怀若谷

    一.显示统计占用系统内存最多的进程,并排序 [root@centos7 ~]# ps -eo uid,pid,ppid,tty,c,time,cmd,%mem --sort=-%mem UID PID ...

  7. LTM_本地流量管理(一)

    基本元素及概念 Node:节点,即服务器的IP地址. Member:成员,即一个服务,用IP+端口表示. Pool:池:一个或多个Member的逻辑分组,一个Pool表示一个应用,每个Pool都有自己 ...

  8. 开发 .swan 文件

    这部分是每个智能小程序页面的展现模板,类似于 Web 开发中的 HTML ,SWAN 模板中使用的标签均为 SWAN 组件规定的标签. <view s-for="item in ite ...

  9. 浅谈IPv4至IPv6演进的实施路径

    作者:个推运维平台网络工程师 宗堂   1 业务背景 在互联网呈现爆炸式发展的今天, IPv4网络地址数量匮乏等问题将会影响到我国的互联网发展与应用,制约物联网.5G等新业务开展.今年4月国家工信部发 ...

  10. 【后台管理系统】—— Ant Design Pro页面相关(一)

    一.List列表形式 import React, { PureComponent } from 'react'; import { findDOMNode } from 'react-dom'; im ...