Books Exchange (easy 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 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 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≤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
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 解题思路:数组值与数组下标相等时即为解,需要注意的是,return结果。 AC代码:
#include <iostream>
using namespace std;
const int maxn=;
int a[maxn];
int cnt=;
int search(int x,int s)
{
if(a[x]==s)
return cnt;
else
{
cnt++;
return search(a[x],s);
}
}
int main()
{
int q,n;
cin>>q;
while(q--)
{ cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
int res;
for(int i=;i<=n;i++)
{
cnt=;
res=search(i,i);
cout<<res<<" ";
}
cout<<endl;
}
return ;
}
第二种使用数组存储结果:
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=;
int a[maxn];
int res[maxn];
int cnt=;
void search(int x,int s)
{
if(a[x]==s)
res[s]=cnt;
else
{
cnt++;
search(a[x],s);
}
}
int main()
{
int q,n;
cin>>q;
while(q--)
{
memset(a,,sizeof a);
memset(res,,sizeof res);
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
{
cnt=;
search(i,i);
}
for(int i=;i<=n;i++)
cout<<res[i]<<" ";
cout<<endl;
}
return ;
}
Books Exchange (easy version) CodeForces - 1249B2的更多相关文章
- CodeForces - 1214D B2. Books Exchange (hard version)
题目链接:http://codeforces.com/problemset/problem/1249/B2 思路:用并查集模拟链表,把关系串联起来,如果成环,则满足题意.之后再用并查集合并一个链,一个 ...
- Books Exchange (hard version)
The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...
- Into Blocks (easy version)
G1 - Into Blocks (easy version) 参考:Codeforces Round #584 - Dasha Code Championship - Elimination Rou ...
- codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]
就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】
任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...
- Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)
F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题
B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...
- Codeforces 1077F1 Pictures with Kittens (easy version)(DP)
题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...
随机推荐
- 6、DockerFile解析:三步走、保留字指令
1.dockerfiel是什么 1.是什么 Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本. 2.构建三步骤 编写Dockerfile文件 docker bu ...
- JDBC (Java DataBase Connectivity)数据库连接池原理解析与实现
一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...
- Ubuntu操作及Linux基础知识
part 1: Ubuntu操作基础 1.调整字体的大小 调大:crtl+shift+“+” 调小:crtl+“-” 2.不要把虚拟机全屏的时候截屏,要不然会认为是Linux系统截屏而非Window ...
- HDU 2176 取(m堆)石子游戏 —— (Nim博弈)
如果yes的话要输出所有情况,一开始觉得挺难,想了一下也没什么. 每堆的个数^一下,答案不是0就是先取者必胜,那么对必胜态显然至少存在一种可能性使得当前局势变成必败的.只要任意选取一堆,把这堆的数目变 ...
- ./与sh区别
1 ./需要执行权限,使用脚本文件中第一行#!指定的shell(解释器)来执行命令(譬如常见的/bin/bash),不指定系统会调用默认shell程序 2 sh不需要执行权限,是使用sh这个s ...
- 2018-2019-2 20165330《网络对抗技术》Exp10 Final 基于PowerShell的渗透实践
目录 实验内容 实验步骤 实验中遇到的问题 实验总结与体会 实验内容 PoweShell简介 PowerShell入门学习 PowerShell渗透工具介绍 相关渗透实践分析 ms15-034之Pow ...
- js判断是否联网
// navigator.onLine if (navigator.onLine){ //正常工作 console.log("在线状态............................ ...
- 通过Maven更换环境配置文件
大致思路:配置文件有三套:main/resources.devmain/resources.prodmain/resources.test公共部分放到main/resource下使用mvn clean ...
- ansible-playbook的YAML语法学习
YAML:可以将你打算对多机器的批量操作放到一个文件中,顺序执行,可以根据机器做到根据机器信息判断执行,其他命令执行结果判断执行. YAML有着严格的层级要求,稍微有个缩进问题就会无法运行,所以学习过 ...
- Android跨进程通信AIDL服务
服务(Service)是android系统中非常重要的组件.Service可以脱离应用程序运行.也就是说,应用程序只起到一个启动Service的作用.一但Service被启动,就算应用程序关闭,Ser ...