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
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

Copy
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 <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 998244353
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int vis[] , a[];
int sum ; void dfs(int x , int i)
{
sum++ ;
if(x == i)
{
vis[x] = sum ;
return ;
}
else
{
x = a[x];
dfs(x , i);
vis[x] = sum ;
}
} int main()
{ int t ;
scanf("%d" , &t);
while(t--)
{
int n ;
scanf("%d" , &n);
for(int i = ; i <= n ; i++)
{
scanf("%d" , &a[i]);
vis[i] = ;
} for(int i = ; i <= n ; i++)
{
sum = ;
if(!vis[i])
dfs(a[i] , i);
} for(int i = ; i < n ; i++)
{
cout << vis[i] << " ";
}
cout << vis[n] << endl ;
} return ;
}
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 998244353
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int vis[] , a[]; int main()
{ int t ;
scanf("%d" , &t);
while(t--)
{
int n ;
scanf("%d" , &n);
for(int i = ; i <= n ; i++)
{
scanf("%d" , &a[i]);
vis[i] = ;
}
queue<int>q;
for(int i = ; i <= n ; i++)
{
if(vis[i]) continue ;
int pos = i ;
do
{
q.push(pos);
pos = a[pos];
}while(pos!=i);
int cnt = q.size();
while(!q.empty())
{
vis[q.front()] = cnt ;
q.pop();
} }
for(int i = ; i < n ; i++)
{
cout << vis[i] << " ";
}
cout << vis[n] << endl ;
} return ;
}

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. "Host 'onlyyou-bridal.jp' is blocked because of many connection errors; unblock with 'mysqladminlush-hosts'"

    错误链接数太多 ,清理mysqladminlush-hosts 这个文件 直接 service mysqld restart  解决了~~~ ccess denied for user 'root'@ ...

  2. java初学第一天

    public class HellowWorld{ public static void main(String[] args){ System.out.println("jiuxu&quo ...

  3. React Native 之 createBottomTabNavigator,createMaterialTopTabNavigator

    icon第三方库 yarn add react-native-vector-icons react-native link react-native-vector-icons 在上次的代码中添加: A ...

  4. 接口返回buffer的16进制数据如何转换

    我们请求接口数据经常会看到buffer数据,这是我们可以使用data.toString()就可以啦~

  5. PHP文件操作基本代码

    PHP中提供了一系列的I/O函数,能简捷地实现我们所需要的功能,包括文件系统操作和目录操作(如“复制[copy]”).下面兄弟连PHP培训 小编给大家介绍的是基本的文件读写操作:(1)读文件 ;(2) ...

  6. Leetcode 1. Two Sum(hash+stl)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. signup图片上传预览经常总结

    html <html> <head> <meta charset="utf-8" /> <meta http-equiv="X- ...

  8. Bazinga

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. 版本基线自动化之windows

    1.背景: 目前项目维护周期过程中,制作调试版本和对外发布版本次数比较频繁,流程过于繁琐和随意,且打包制作人成为瓶颈,为了规范版本基线流程和实现全员自动化参与,拟定版本基线自动化方案. 2.目标: 版 ...

  10. [CSP-S模拟测试]:e(树上主席树)

    题目传送门(内部题66) 输入格式 第一行,一个正整数$n$,一个自然数$q$,一个整数$type$.第二行,$n$个正整数,代表$a_i$.接下来$n-1$行,每行两个正整数$u$.$v$,代表树中 ...