CodeForces1249B1/B2-Books Exchange-dfs-一般搜索+记忆化搜索
一般搜索
注意:一般定义成void
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 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
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 题意:
给出t组数,每组给出n,表示接下来有n本书,a[i]=k,表示第i个人需要把手上的书给第k个人。
问:编号为i的书回到第i个人手里需要多少次。 看第二组样例输入2 3 1,输出3 3 3。
- sum1=0,
第1个人把手上的书给第2个人,sum++,第二个人把书给第3个人,sum++,第三个人把书给第1个人,sum++,所以sum1=3;
- sum2=0,
第2个人把手上的书给第3个人,sum++,第3个人把书给第1个人,sum++,第1个人把书给第2个人,sum++,所以sum2=3;
- sum3=0,
第3个人把手上的书给第1个人,sum++,第1个人把书给第2个人,sum++,第2个人把书给第3个人,sum++,所以sum3=3;
思路: 开一个for循环,然后dfs(i,i,1)
i:第i个人
i:第i个人所对应下次把书传到a[i]手上去
1:代表书转手次数=1,第一次书在自己手上,所以次数为1,以后每次搜索次数++ 开始进行搜索
void dfs(int x,int k,int t)//传入下标x,下标k,转手次数t
{
if(w)
return;//剪枝
if(x==a[k])//此时表示第x个人找到了对应a[k]=x的书
{
w=t;//定义w为全局变量,w=t,记录转手次数
return ;
}
dfs(x,a[k],t+);//否则还未找到,继续进行搜索
}
代码如下:
#include<stdio.h>
int a[],w; void dfs(int x,int k,int t)
{
if(w)
return;
if(x==a[k])
{
w=t;
return ;
}
dfs(x,a[k],t+);
} int main()
{
int t,m;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
w=;
dfs(i,i,);
printf("%d ",w);
}
printf("\n");
}
return ;
}
记忆化搜索
注意:一般定义成int,需要有返回值,表示之前已经搜到过的值,无需再搜
Books Exchange (hard 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 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
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 思路:
由于数据变大了,所以按照上面的方法进行深搜会超时,所以需要用到记忆化搜素。
开一个book数组,book[x]=w,表示书回到第x个人手上需要转手w次。
假设输入2 3 1,a[i]=k的书回到第k个人手中需要t次的话,那么在转手过程中所经过的所有点k回到第k个人手中也需要t次,
所以开一个b数组,把所经过的所有k全部保存下来,for循环往下走走到该点就不需要再搜,直接输出即可
利用book数组,把b数组里面存的所有数(有p个)通过循环赋值为k,即book[ b[i] ]=w。
代码如下:
#include<stdio.h>
#include<string.h>
const int N=2e5+;
int a[N],w,b[N],p,book[N]; void dfs(int x,int k,int t)
{
if(x==a[k])
{
w=t;
book[x]=w;
return;
// return book[x]=t;
}
b[p++]=a[k];//把走过的点全部存起来,下次碰到不用再走就不会超时
dfs(x,a[k],t+);
} int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(book,,sizeof(book));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
p=;
if(i!=)
printf(" ");
if(book[i])
{
printf("%d",book[i]);
continue;
}
// w=0;
dfs(i,i,);
for(int j=;j<p;j++)
book[b[j]]=w;
printf("%d",w);
}
printf("\n");
}
return ;
}
CodeForces1249B1/B2-Books Exchange-dfs-一般搜索+记忆化搜索的更多相关文章
- skiing(搜索+记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- 【蓝桥杯真题】地宫取宝(搜索->记忆化搜索详解)
链接 [蓝桥杯][2014年第五届真题]地宫取宝 题目描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被 ...
- P4363 [九省联考2018]一双木棋chess(对抗搜索+记忆化搜索)
传送门 这对抗搜索是个啥玩意儿…… 首先可以发现每一行的棋子数都不小于下一行,且局面可由每一行的棋子数唯一表示,那么用一个m+1进制数来表示当前局面,用longlong存,开map记忆化搜索 然后时间 ...
- hdu1078 dfs+dp(记忆化搜索)搜索一条递增路径,路径和最大,起点是(0,0)
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...
- 数位dp/记忆化搜索
一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an ...
- 蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索
问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...
- 【noip 2009】 乌龟棋 记忆化搜索&动规
题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...
- hdu 1428(很好的一道题,最短路+记忆化搜索)
漫步校园 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ1088滑雪(记忆化搜索)
就是用DP,DP[i][j]是在这个(i,j)位置作为起点的最长长度. 因为可能会超时,DP的话每次就是记录,然后就不用回溯了. 很简单的DFS里面的记忆化搜索. #include <stdio ...
随机推荐
- 「NOI2016」循环之美 解题报告
「NOI2016」循环之美 对于小数\(\frac{a}{b}\),如果它在\(k\)进制下被统计,需要满足要求并且不重复. 不重复我们确保这个分数是最简分数即\((a,b)=1\) 满足要求需要满足 ...
- 使用PaxScript为Delphi应用增加对脚本的支持
通过使用PaxScript可以为Delphi应用增加对脚本的支持. PaxScript支持paxC,paxBasic,paxPascle,paxJavaScript(对ECMA-262做了扩展) 四种 ...
- __iomem作用
最近在看网卡驱动时查看ioremap函数发现调用最低层用__iomem修饰了ioremap的第一个参数(unsigned int)ioremap(S3C24XX_PA_CS8900, SZ_1M) + ...
- 戏说 .NET GDI+系列学习教程(三、Graphics类的方法的总结)
- SQL比较时间查询语句
select * from table1 where datediff(mm,'2009-8-12 13:17:50', date)>0 select * from table1 select ...
- 4、APP FPS测试
什么是FPS FPS是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数.FPS是测量用于保存.显示动态视频的信息数量.每秒钟帧数愈多,所显示的动作就会愈流畅.通常,要避免动作不 ...
- synchronized与ReenTranLock的区别
1.synchronized 回顾 表示原子性和可见性 原子性:一次只有一个线程能执行lock保护的代码 可见性:线程更新了变量后会将其更新到主内存里面 volatile可以实现可见性,不能实现原子性 ...
- C#基础-->cookie和session
关于cookie和session cookie 1:一个cookie中可以存放的数据最大在4KB左右 2:cookie存放于客户端 3:cookie分为两种 一种是会话cookie 一种是持久co ...
- Codeforces Round #536 E. Lunar New Year and Red Envelopes /// 贪心 记忆化搜索 multiset取最大项
题目大意: 给定n m k:(1≤n≤1e5, 0≤m≤200, 1≤k≤1e5) 表示n个时间长度内 最多被打扰m次 k个红包 接下来k行描述红包 s t d w:(1≤s≤t≤d≤n , 1≤w≤ ...
- 如何去实现一个运用于多平台的SDK ?
开始要求实现一个SDk的时候,一脸懵逼,以前总是调别人的SDK暴露的接口与方法,现在自己去实现一个,可以用到各平台上,还是相当有难度的,经过大半月的研究还是有点眉目的,想在这里和大家分享一下鄙人简陋的 ...