dfs(找环)
https://codeforces.com/problemset/problem/1249/B2
1 second
256 megabytes
standard input
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.
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).
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.
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
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(找环)的更多相关文章
- # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)
「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...
- 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 ...
- CodeForces - 103B(思维+dfs找环)
题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- 与图论的邂逅06:dfs找环
当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...
- HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)
求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...
- UVaLive 6950 && Gym 100299K Digraphs (DFS找环或者是找最长链)
题意:有n个只包含两个字母的字符串, 要求构造一个m*m的字母矩阵, 使得矩阵的每行每列都不包含所给的字符串, m要尽量大, 如果大于20的话构造20*20的矩阵就行了. 析:开始吧,并没有读对题意, ...
- [NOI2008]假面舞会——数论+dfs找环
原题戳这里 思路 分三种情况讨论: 1.有环 那显然是对于环长取个\(gcd\) 2.有类环 也就是这种情况 1→2→3→4→5→6→7,1→8→9→7 假设第一条链的长度为\(l_1\),第二条为\ ...
- [蓝桥杯2018初赛]小朋友崇拜圈(dfs找环)
传送门 思路: 题意大意:n条有向边,找出最大环. 我们发现,如果一个小朋友没有被任何人崇拜,那么他一定不位于环中.为此我们可以设置一个indug数组预处理.如果2被崇拜了那么indug[2]就加加, ...
- New Reform---cf659E(dfs找环)
题目链接:http://codeforces.com/problemset/problem/659/E 给你n个点,m条双向边,然后让你把这些边变成有向边,使得最后的图中入度为0的点的个数最少,求最少 ...
随机推荐
- 【vue-cli 3.0】 vue.config.js配置 - 路径别名
如何配置vue-cli 3中vue.config.js的路径别名? 前段时间更新电脑重装了一下vue-cli,发现了vue-cli已经更新到3.0版.用来搭建项目后发现简化了很多,而且配置文件现在可以 ...
- React使用的思考总结
1.事件处理中的this指针问题 在 react 中,用 class 声明一个组件,调用 class 中的方法时,如果该方法中有 this 且没有手动绑定 this 指针,则会发生 this 指向 u ...
- Kohana重写接收不到get参数问题
.htaccess,不需要重启apache # Turn on URL rewriting RewriteEngine On # Installation directory RewriteBase ...
- QT开发安卓APP的中文字体问题
1.安卓默认的字体为DroidSansFallback:谷歌中文字体 由于手机和PC字体的不一致,导致PC上开发的APP到目的安卓设备中文显示为小方框. 故需要要在qt的main函数中设置系统字体: ...
- All-one Matrices
All-one Matrices 单调栈 最大全一矩阵计数 #include<bits/stdc++.h> #define maxn 3005 using namespace std; # ...
- Vue中的MVVM框架
ViewModel:数据双向绑定 场景: 针对具有复杂交互逻辑的前段应用 提供基础的架构抽象 通过Ajax数据持久化,保证前端用户体验 什么是vue.js? 是一个轻量级的mvvm框架 数据驱动+组 ...
- 在Ubuntu18.04下安装Java 11
一直以来,本人都使用第三方软件包"ppa:linuxuprising/java"安装Java JDK,最近一次安装时发现无法成功.这是由于现在无法直接从Oracle官网下载Java ...
- openocd安装与调试
环境: 硬件:PC机<------>ARM仿真器v8.00<------>已下载好bit流的Xinlinx SoC开发板(其上有arm cortex-a9核) 软件:Redha ...
- nginx代理前端项目
参考:https://segmentfault.com/a/1190000013980557 https://segmentfault.com/a/1190000013267839 注意 1.只能有 ...
- ArrayDeque 源码分析
ArrayDeque ArrayDeque 能解决什么问题?什么时候使用 ArrayDeque? 1)Deque 接口大小可变的循环数组实现,ArrayDeque 没有容量限制并会按需增长. 2)Ar ...